php不破坏单词截取子字符串
- /*
- snippet(phrase,[max length],[phrase tail])
- snippetgreedy(phrase,[max length before next space],[phrase tail])
- */
- http://www.ynmxzx.com/zxzx/ybzx/20160614/3709.html
- 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,0,$length) . $tail;
- }http://www.ynmxzx.com/zxzx/ybzx/20160614/3710.html
- }
- $text = substr($text,0,$length-$i+1) . $tail;
- }
- return $text;
- }
- // It behaves greedy, gets length characters ore goes for more
- function snippetgreedy($text,$length=64,$tail="...") {
- $text = trim($text);
- if(strlen($text) > $length) {
- for($i=0;$text[$length+$i]!=" ";$i++) {
- if(!$text[$length+$i]) {
- return $text;
- }http://www.ynmxzx.com/zxzx/ybzx/20160614/3711.html
- }
- $text = substr($text,0,$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=64,$tail="...") {
- $text = trim($text);
- $txtl = strlen($text);
- if($txtl > $length) {
- for($i=1;$text[$length-$i]!=" ";$i++) {
- if($i == $length) {
- return substr($text,0,$length) . $tail;
- }http://www.ynmxzx.com/qyd/20160614/3712.html
- }
- for(;$text[$length-$i]=="," || $text[$length-$i]=="." || $text[$length-$i]==" ";$i++) {;}
- $text = substr($text,0,$length-$i+1) . $tail;
- }
- return $text;
- }
- http://www.ynmxzx.com/_wccxs/20160614/3713.html
- /*www.ynmxzx.com
- 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, perhaps, or perhaps yes, no idea") . "<br>");
- echo(snippetgreedy("this is not too long to run on the column on the left, perhaps, or perhaps yes, no idea"));
- */