note 44475 rejected from function.str-word-count by tomsommer
| From: | tomsommer@php.net | Date: | Sat, 07 Aug 2004 03:53:10 +0000 |
| Subject: | note 44475 rejected from function.str-word-count by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-74122@lists.php.net to get a copy of this message | ||
Note Submitter: mail@southwinddesign.com
----
str_word_count works great on alaphabetic strings (if you clean the text first) but doesn't
include numbers (numeric strings). I couldn't use it on a recent project because I needed to
count numeric strings (phone numbers, etc) as words.
Default str_word_count
// remove excess spaces between words
$string = ereg_replace(" +"," ","$string");
// remove leading and trailing spaces
$string = trim($string);
// count number of words
$numberWords = str_word_count($string);
The above was inaccurate for my purpose so to count the number of elements in an alphanumeric string
clean the data then create and count an array.
$string = ereg_replace(" +", " ", $string);
$string = trim($string);
// count number of words
$wordArray = explode(" ", $string);
$numberWords = count ($wordArray);
Thanks for all the help, hope I'm giving something back.