The example in this article describes the method of intercepting strings by words in php. Share it with everyone for your reference. The specific analysis is as follows:
Specify the number of strings and words here to intercept
Copy code The code is as follows:
function limit_words($string, $word_limit)
{
$words = explode(" ",$string);
Return implode(" ",array_splice($words,0,$word_limit));
}
//Example Usage
$content = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
echo limit_words($content,20);
?>