note 27598 added to function.substr
| From: | igjav at cesga dot es | Date: | Thu, 12 Dec 2002 16:13:05 +0000 |
| Subject: | note 27598 added to function.substr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-40792@lists.php.net to get a copy of this message | ||
/*
Seems to return the initial ($length) portion of text,
but taking words as atoms delimited by spaces, without
fragmenting them.
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") . "<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"));
*/
--
http://www.php.net/manual/en/function.substr.php
http://master.php.net/manage/user-notes.php?action=edit+27598
http://master.php.net/manage/user-notes.php?action=delete+27598
http://master.php.net/manage/user-notes.php?action=reject+27598