supanoob Posted March 16, 2009 Share Posted March 16, 2009 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/149725-solved-counting-words-in-a-tring/ Share on other sites More sharing options...
Mark Baker Posted March 16, 2009 Share Posted March 16, 2009 You could always try using PHP's built-in str_word_count() function Quote Link to comment https://forums.phpfreaks.com/topic/149725-solved-counting-words-in-a-tring/#findComment-786244 Share on other sites More sharing options...
supanoob Posted March 16, 2009 Author Share Posted March 16, 2009 You could always try using PHP's built-in str_word_count() function yeah but that includes spaces if someone double spaces doesnt it? Quote Link to comment https://forums.phpfreaks.com/topic/149725-solved-counting-words-in-a-tring/#findComment-786246 Share on other sites More sharing options...
Mark Baker Posted March 16, 2009 Share Posted March 16, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/149725-solved-counting-words-in-a-tring/#findComment-786248 Share on other sites More sharing options...
supanoob Posted March 16, 2009 Author Share Posted March 16, 2009 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 worked fine Quote Link to comment https://forums.phpfreaks.com/topic/149725-solved-counting-words-in-a-tring/#findComment-786251 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.