Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.