Home> php教程> PHP源码> body text

php intercepts substring without destroying words

大家讲道理
Release: 2016-11-11 10:24:56
Original
1315 people have browsed it

/* 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,0,$length) . $tail; } } $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; } } $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; } } for(;$text[$length-$i]=="," || $text[$length-$i]=="." || $text[$length-$i]==" ";$i++) {;} $text = substr($text,0,$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") . "
"); echo(snippetwop("this is not too long to run on the column on the left, perhaps, or perhaps yes, no idea") . "
"); echo(snippetgreedy("this is not too long to run on the column on the left, perhaps, or perhaps yes, no idea")); */
Copy after login

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
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!