note 27418 added to function.strtok
| From: | shaun at phplabs dot com | Date: | Thu, 05 Dec 2002 03:57:17 +0000 |
| Subject: | note 27418 added to function.strtok | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-40369@lists.php.net to get a copy of this message | ||
Here's some code to extract the first part of a long paragraph, e.g. to use as a summary.
Starting at the beginning of the paragraph it gets as many complete sentences as are necessary to
contain $limit words. For example, with $limit at 20 it would return the first two sentences of the
paragraph you're reading right now (the first 20 words plus the rest of the sentence in which
the limit was hit).
function summarize($paragraph, $limit){
$tok = strtok($paragraph, " ");
while($tok){
$text .= " $tok";
$words++;
if(($words >= $limit) && ((substr($tok, -1) == "!")||(substr($tok, -1) ==
".")))
break;
$tok = strtok(" ");
}
return ltrim($text);
}
Might be a better way to do this, but it worked for me. Hope you find it useful!
--
http://www.php.net/manual/en/function.strtok.php
http://master.php.net/manage/user-notes.php?action=edit+27418
http://master.php.net/manage/user-notes.php?action=delete+27418
http://master.php.net/manage/user-notes.php?action=reject+27418