note 21736 deleted from function.count-chars by didou
| From: | didou@php.net | Date: | Mon, 20 Oct 2003 11:56:46 +0000 |
| Subject: | note 21736 deleted from function.count-chars by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-58936@lists.php.net to get a copy of this message | ||
Note Submitter: jamesNOSPAM@medsinNOSPAM.org
----
Ok, I'm stupid, so it took me ages to work out how to count words using this function. Anyway,
got it to work well like this:
function count_words($string) {
$result = count_chars($string, 0);
$words = $result[32];
return($words);
}
'32' being, apparently, the character number for space. So it actually counts the number
of spaces, but this is generally a good estimate. Checked some counts using MS Word, and they were
generally close. If your users like big rows of spaces may not be so good though...
Put more neatly and with added rounding to the nearest 10 (requires php 4.0):
function count_words($string) {
$result = count_chars($string, 0);
return(round($result[32], -1));
}
Anyone else using PostNuke - if you pass this the $info[bodytext] variable then it works well as a
replacement for "X bytes more" in your theme.