note 59170 deleted from function.str-word-count by bobby
| From: | bobby@php.net | Date: | Fri, 06 Oct 2006 18:12:42 +0000 |
| Subject: | note 59170 deleted from function.str-word-count by bobby | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-118147@lists.php.net to get a copy of this message | ||
Note Submitter: webmaster at joshstmarie dot com
----
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.