Home > Article > Backend Development > PHP restricts long sentence titles and replaces the following words with '...' code_PHP tutorial
//This code can be used to limit a very long sentence and replace the following words with "...", which is suitable for limiting the length of article titles
$str="This is a verrrrrrryyyyy long string. This is a verrrrrryyyyy long string.";//Long string
$no_of_words = 7;//Set the limited number of words
$str_array = split(" ",$str,$no_of_words+ 1);// Split sentences with spaces
if(count($str_array)>$no_of_words) {// Determine whether the number of words in the original string is greater than the set number of words
for($i= 0; $i print $str_array[$i]." "; } //Print the words one by one until the set value Fixed value
print ".....(more)";} //When the set value is reached, print the ellipsis...and add a more as a prompt
else { // Otherwise, if the number of words in the original string is less than the set value
print $str; }//Then of course the entire string will be printed, well, it goes without saying.
?>