Count words using Regex

By | 29 March, 2011

Yes, i know, very very easy. But i am leaving this here as reference:

$texto = “how to count words in a very simple step”;

$contagem = preg_match_all(“/\w+/”, $texto, $blaharray);
echo $contagem; // 9

For further reading:

Regular Expressions info

preg_match()

preg_match_all()