It depends on what you define as a word, but let's take the simple approach
of stripping HTML et al, and exploding the string on the spaces, then
counting the array of words:
<?
$str = "This is my <b> text </b> that I want to count";
$words = explode(' ', strip_tags($str));
$count = count($words);
?>
Really no need for regexp's here IMHO.
Justin
on 06/04/03 11:46 PM, Sebastian ([EMAIL PROTECTED]) wrote:
> hello,
>
> I am trying to count the words from text that is fetched from mysql, It
> works, but it's a little inaccurate.. It's about 5 - 10 words off, My guess
> it's probably counting some HTML or something. Is there a more accurate way
> of counting words?
>
> $row[12] = strip_tags(strtr($row[12],
> array_flip(get_html_translation_table(HTML_ENTITIES))));
> $wordcount = preg_match_all("#(\w+)#", $row[12], $match_dummy);
>
> if($wordcount == "0") {
> $count ="";
> } else {
> $count = preg_match_all("#(\w+)#", $row[12], $match_dummy);
> }
>
>
> cheers,
> - Sebastian
>
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php