Re: counting words.

From: Date: Fri, 18 Apr 2003 17:24:07 +0000
Subject: Re: counting words.
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-144418@lists.php.net to get a copy of this message
On Mon, 7 Apr 2003 00:37:53 +0000 (GMT) Philip Olson wrote: > Simply exploding on ' ' will typically be off because > some words are separated by double spaces, newlines, > tabs, etc. This is why using regular expressions on > whitespace is more accurate. This is also why there > are a million ways to get a word count :) Why not going the other route - instead of separating by white space and checking how many elements are left, separate by your definition of 'word'. The following example works for me: $str="This is one <b>test</b> another <div align=center>test-driven</div> one's expamle."; $str = preg_replace ("|<[^>]+>|U", "", $str); echo $str."<br>"; $count = preg_match_all ("#[A-z]+([-]?[a-z]+|[']?[a-z]+)?#", $str, $arr); echo "preg_match_all(): ".$count; Of course, I have not testing it with a large string but I think it would work. Furthermore, depending on your definition of 'word', the second regular expression might become too complicates. HTH, -- Mitko -- ======================================================================== __ __ __________________ _________ / |/ |/ /__/__ ___/ |/ / / Dimitar Haralanov / / / / / / / / / Software Engineer / / / / / / / / /\ \ / / Tahoe Networks /___/___/__/__/ /__/ /__/ |__|___/ mitko@tahoenetworks.com ======================================================================== GPG Fingerprint: 21C1 8244 93F3 F2E9 013C 0C12 70DC C58E 6337 ABD1 ------------------------------------------------------------------------ To teach is to learn.

Attachment: [application/pgp-signature]
« previous php.general (#144418) next »