mdmartiny Posted March 3, 2012 Share Posted March 3, 2012 I am writing a function to count the words that have been typed into a text box. I originally had it working when I first wrote it but it was counting all of the punctuation as a word so I I tried adding some code to eliminate that. Now it is not working. Could someone help me? Here is my code function count_words($description){ $word_count = 0; $description = trim(preg_replace("/\s+/"," ",$description)); $word_array = explode(" ", $description); for($i=0; $i < count($word_array); $i++){ if (preg_match("[0-9A-Za-z]", $word_array[$i])) { $word_count++; } return $word_count; } } Quote Link to comment Share on other sites More sharing options...
scootstah Posted March 3, 2012 Share Posted March 3, 2012 You can use str_word_count, and remove any punctuation first. function count_words($description) { // remove punctuation $punctuation = array('.', ',', '!', '?', ':', ';', '(', ')'); $description = str_replace($punctuation, '', $description); return str_word_count($description); } Quote Link to comment Share on other sites More sharing options...
ignace Posted March 3, 2012 Share Posted March 3, 2012 You can use str_word_count, and remove any punctuation first. function count_words($description) { // remove punctuation $punctuation = array('.', ',', '!', '?', ':', ';', '(', ')'); $description = str_replace($punctuation, '', $description); return str_word_count($description); } No need to remove the punctuation, str_word_count is intelligent enough to know that punctuation is not a word. echo str_word_count($description); Quote Link to comment Share on other sites More sharing options...
mdmartiny Posted March 3, 2012 Author Share Posted March 3, 2012 I made the changes to it.. Now all I am is getting the number 0 when I should be getting the number 2. Quote Link to comment Share on other sites More sharing options...
ignace Posted March 3, 2012 Share Posted March 3, 2012 Post your code. Why did you set the topic to solved if you are still having an issue? Quote Link to comment Share on other sites More sharing options...
mdmartiny Posted March 3, 2012 Author Share Posted March 3, 2012 function count_words($description){ $word_count = 0; $punctuation = array('.', ',', '!', '?', ':', ';', '(', ')'); $description = str_replace($punctuation, '', $description); $description = trim(preg_replace("/\s+/"," ",$description)); $word_array = explode(" ", $description); for($i=0; $i < count($word_array); $i++){ if (preg_match("[0-9A-Za-z]", $word_array[$i])) { $word_count++; } return $word_count; } } Quote Link to comment Share on other sites More sharing options...
ignace Posted March 3, 2012 Share Posted March 3, 2012 You don't need the count_words function just use str_word_count like i showed you earlier. Quote Link to comment Share on other sites More sharing options...
mdmartiny Posted March 3, 2012 Author Share Posted March 3, 2012 oh ok ..... I thought that had to be part of the function... Thank You Quote Link to comment 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.