Home >Backend Development >PHP Tutorial >PHP restricts long sentence titles and replaces the following words with '...' code_PHP tutorial

PHP restricts long sentence titles and replaces the following words with '...' code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:23:58952browse


//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.
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532179.htmlTechArticle//This code can be used to limit a long sentence and replace it with "..." words, suitable for limiting the length of article titles $str="This is a verrrrrrryyyyy long string. This i...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn