|
Posted by Oliver Saunders on 11/16/05 13:18
I was really shocked not to find this already submitted by someone in
the PHP manual notes.
<?
function jnWordCount($text) {
$results = array();
$pattern = '/[\w0-9]+\S+/'; // matches wordy characters and numbers
preg_match_all($pattern,trim($text),$results);
return count($results[0]);
}
$sentence = ' Should be - (ready) ( 53 ) . , =-';
echo jnWordCount($sentence); // outputs 4
// matches: array('Should','be','ready)','53')
?>
It counts the number of words ignoring non-wordy charcters and extra
whitespace. Infintately better than:
<?
echo count(explode(' ',$sentence));
?>
Which really only counts the number of spaces not the number of words at
all. The reason I'm posting this here is there is just one slight
problem. I'm pretty new to regular expressions and they never seem to do
exactly what I want.
It doesn't seem to want to match single character words.
<?
echo jnWordCount('a 41 I 5'); // outputs 1. A match for '41' only
?>
Ideas?
[Back to original message]
|