php不破坏单词截取子字符串
发布时间:2020-05-25 07:28:31 所属栏目:PHP 来源:互联网
导读:php不破坏单词截取子字符串
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 /*
snippet(phrase,[max length],[phrase tail])
snippetgreedy(phrase,[max length before next space],[phrase tail])
*/
function snippet($text,$length=64,$tail="...") {
$text = trim($text);
$txtl = strlen($text);
if($txtl > $length) {
for($i=1;$text[$length-$i]!=" ";$i++) {
if($i == $length) {
return substr($text,$length) . $tail;
}
}
$text = substr($text,$length-$i+1) . $tail;
}
return $text;
}
// It behaves greedy,gets length characters ore goes for more
function snippetgreedy($text,$tail="...") {
$text = trim($text);
if(strlen($text) > $length) {
for($i=0;$text[$length+$i]!=" ";$i++) {
if(!$text[$length+$i]) {
return $text;
}
}
$text = substr($text,$length+$i) . $tail;
}
return $text;
}
// The same as the snippet but removing latest low punctuation chars,// if they exist (dots and commas). It performs a later suffixal trim of spaces
function snippetwop($text,$length) . $tail;
}
}
for(;$text[$length-$i]=="," || $text[$length-$i]=="." || $text[$length-$i]==" ";$i++) {;}
$text = substr($text,$length-$i+1) . $tail;
}
return $text;
}
/*
echo(snippet("this is not too long to run on the column on the left,perhaps,or perhaps yes,no idea") . "<br>");
echo(snippetwop("this is not too long to run on the column on the left,no idea") . "<br>");
echo(snippetgreedy("this is not too long to run on the column on the left,no idea"));
*/
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
