Jump to content

[SOLVED] Counting Words in a tring


supanoob

Recommended Posts

I have the following code that should count the words in a string when submitted through a form, the string is being sent ok but the function isnt counting the words, any idea why?

if ($_GET['step'] == 'websites_process')
{
$site_name = $_POST['name'];
$site_url = $_POST['url'];
$site_desc = $_POST['description'];
$site_catagory = $_POST['website_catagory'];
$screenshot = $_POST['screenshot'];
//remove once i know all variables are being sent
echo "$site_name<br>$site_url<br>$site_desc<br>$site_catagory<br>$screenshot";

function adv_count_words($site_desc){
     $words = 0;
     $site_desc = eregi_replace(" +", " ", $site_desc);
     $array = explode(" ", $site_desc);
     for($i=0;$i < count($array);$i++)
	 {
         if (eregi("[0-9A-Za-zÀ-ÖØ-öø-ÿ]", $array[$i])) 
             $words++;
     }
}
//remove once i know the words are being counted correctly
echo "$words";
}

 

 

Link to comment
https://forums.phpfreaks.com/topic/149725-solved-counting-words-in-a-tring/
Share on other sites

You could always try using PHP's built-in str_word_count() function

 

yeah but that includes spaces if someone double spaces doesnt it?

No

 

$string = "The    quick brown fox    jumps over   the lazy    dog";

$counts = str_word_count($string);

You could always try using PHP's built-in str_word_count() function

 

yeah but that includes spaces if someone double spaces doesnt it?

No

 

$string = "The    quick brown fox    jumps over   the lazy    dog";

$counts = str_word_count($string);

 

Thanks :D worked fine

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.