Reply to Word Count

Your name:

Reply:


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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация