Jump to content

Word count function improvement


chomedey

Recommended Posts

I'm using this function to return a word count on a user-submitted entry:

 

function adv_count_words($str){ // used to count words for stats

    $words = 0;

    $str = eregi_replace(" +", " ", $str);

    $array = explode(" ", $str);

    for($i=0;$i < count($array);$i++)

{

        if (eregi("[0-9A-Za-zÀ-ÖØ-öø-ÿ]", $array[$i]))

            $words++;

    }

    return $words;

}

 

The only problem is that when I write one word, followed by a carriage return, and then another word, followed by a carriage return, etc., the function returns a value of one.

 

Does anyone know how to fix this?

 

I also hear that eregi is a deprecated function.  Does anyone know what I should be using instead?

 

Thanks.

 

Julian

Link to comment
https://forums.phpfreaks.com/topic/193212-word-count-function-improvement/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.