Home > php教程 > php手册 > php限制长句标题,并以“......”代替后面单词的代码

php限制长句标题,并以“......”代替后面单词的代码

WBOY
Release: 2016-06-13 10:23:58
Original
919 people have browsed it


//这个代码可用来限制一句很长的话,并以"....."来代替后面的单词,适合用来限制文章标题的长度
$str="This is a verrrrrrryyyyy long string. This is a verrrrrrryyyyy long string.";//长字串
$no_of_words = 7;// 设定限制的单词个数
$str_array = split(" ",$str,$no_of_words+1);// 以空格来分割句子
if(count($str_array)>$no_of_words) {// 判断原字串单词个数是否大于设定的单词个数
for($i=0; $i print $str_array[$i]." "; } //将单词一个个列印出来直到设定值
print ".....(more)";} //达到设定值时就列印省略号......并加一个more做提示
else { // 否则如果原字串的单词个数小于设定值
print $str; }//那当然就列印出整个字串,咳,这还用说。
?>

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template