note 21736 added to function.count-chars

From: Date: Fri, 24 May 2002 20:01:47 +0000
Subject: note 21736 added to function.count-chars
Groups: php.notes 
Request: Send a blank email to php-notes+get-31007@lists.php.net to get a copy of this message
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. -- http://www.php.net/manual/en/function.count-chars.php http://master.php.net/manage/user-notes.php?action=edit+21736 http://master.php.net/manage/user-notes.php?action=delete+21736 http://master.php.net/manage/user-notes.php?action=reject+21736

« previous php.notes (#31007) next »