note 44475 added to function.str-word-count
| From: | mail at southwinddesign dot com | Date: | Sat, 07 Aug 2004 03:40:00 +0000 |
| Subject: | note 44475 added to function.str-word-count | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-74120@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/function.str-word-count.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+44475
Delete -- http://master.php.net/manage/user-notes.php?action=delete+44475&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+44475&report=yes
Search -- http://master.php.net/manage/user-notes.php