Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Php Programming Code Examples

Php > Arrays Code Examples

Counting Words in Alpanumeric Strings

Counting Words in Alpanumeric Strings 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 $numwords = str_word_count($string); ?> The above wass 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 $numwords = explode(" ", $string); $numwords = count ($numwords); ?> Thanks for all the help, hope I'm giving something back.