note 59170 added to function.str-word-count
| From: | webmaster at joshstmarie dot com | Date: | Mon, 28 Nov 2005 13:29:47 +0000 |
| Subject: | note 59170 added to function.str-word-count | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-99240@lists.php.net to get a copy of this message | ||
I re-examined the methods I used in my last script, as well as the other methods shown on the
comments in this page. I've revised my code a great deal since then.
This new version, will limit paragraphs to a certain amount of words. It's the fastest possible
way I can see of doing it... so here it is:
// Truncation is by word limit, not character limit. So if you limit
// to 255, then it will cut off a text item to 255 words, not 255 characters
function trim_text ($string, $truncation=250) {
$string = preg_split("/\s+/",$string,($truncation+1));
unset($string[(sizeof($string)-1)]);
return implode(' ',$string);
}
By avoiding array_slice, it is much faster. Correcting for multiple spaces still slows it down on
large text items, but it's still blazing quick in the end.
----
Manual Page -- http://www.php.net/manual/en/function.str-word-count.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+59170
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59170&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59170&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59170&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59170&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59170&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59170&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59170&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+59170&report=yes
Search -- http://master.php.net/manage/user-notes.php