sonnieboy Posted March 8, 2010 Share Posted March 8, 2010 Hello everyone, I would like to count how words that do *not* have any of the words in array $frags in them. Can you assist, please?<?php $mystring = "Some apple is eating my orange but not my watermelon."; function notInList($string) { $frags = array('as', 'is', 'about', 'us', 'at'); $parts = explode(' ', strtolower($string)); $count = 0; foreach ($parts as $part) { if (in_array($part[0], $frags)) { echo "$part<br />"; $count++; } } return $count; } echo notInList($mystring)." words"; ?> I am pretty much a newbie to php; sorry. Thanking you in advance. Link to comment https://forums.phpfreaks.com/topic/194547-words-not-in-list/ Share on other sites More sharing options...
xnowandtheworldx Posted March 8, 2010 Share Posted March 8, 2010 <?php $mystring = "Some apple is eating my orange but not my watermelon about."; function notInList($string) { $frags = array('as', 'is', 'about', 'us', 'at'); $words = explode(' ', strtolower($string)); $count = 0; $replace = array(',','.','\'','!'); //replace common punctuation so that it can still recognize words foreach($words as $word) { if(in_array(str_replace($replace, '', $word), $frags)) { echo str_replace($replace, '', $word) . '<br/>'; $count++; } } return $count; } echo notInList($mystring) . " words."; ?> I commented where you had made your mistake. EDIT: I also made it to remove punctuation so that it can still recognize the word even if it has an ',!. at the end of it. Link to comment https://forums.phpfreaks.com/topic/194547-words-not-in-list/#findComment-1023202 Share on other sites More sharing options...
sonnieboy Posted March 8, 2010 Author Share Posted March 8, 2010 Thanks alot xnowandtheworldx for the prompt response. I wanted to see a count of words that don't have any of these words: $frags = array('as', 'is', 'about', 'us', 'at'); Thanks, Link to comment https://forums.phpfreaks.com/topic/194547-words-not-in-list/#findComment-1023212 Share on other sites More sharing options...
xnowandtheworldx Posted March 8, 2010 Share Posted March 8, 2010 Thanks alot xnowandtheworldx for the prompt response. I wanted to see a count of words that don't have any of these words: $frags = array('as', 'is', 'about', 'us', 'at'); Thanks, Ah, okay, sorry about that, well, if you can't figure out how to change it post here, and I will post the updated code and let you know how I set it up. Link to comment https://forums.phpfreaks.com/topic/194547-words-not-in-list/#findComment-1023216 Share on other sites More sharing options...
sonnieboy Posted March 8, 2010 Author Share Posted March 8, 2010 hi xnowandtheworldx, if you don't mind, can you please post me the updated version? Again, thanks very much. Link to comment https://forums.phpfreaks.com/topic/194547-words-not-in-list/#findComment-1023258 Share on other sites More sharing options...
sonnieboy Posted March 9, 2010 Author Share Posted March 9, 2010 can anyone, please help, please? Link to comment https://forums.phpfreaks.com/topic/194547-words-not-in-list/#findComment-1023329 Share on other sites More sharing options...
xnowandtheworldx Posted March 9, 2010 Share Posted March 9, 2010 can anyone, please help, please? <?php $mystring = "Some apple is eating my orange but not my watermelon about."; function notInList($string) { $frags = array('as', 'is', 'about', 'us', 'at'); $words = explode(' ', strtolower($string)); $replace = array(',','.','\'','!'); //replace common punctuation so that it can still recognize words $count = 0; foreach($words as $word) { if(!in_array(str_replace($replace, '', $word), $frags)) { //All you had to do was add an ! in front of the in_array, so essentially, it is now trying make sure that the words in $frags are NOT in the string...if that makes sense? echo str_replace($replace, '', $word) . '<br/>'; $count++; } } return $count; } echo notInList($mystring) . " words."; ?> I believe this is what you wanted, if it's not just let me know, and I'll see what I can do. Link to comment https://forums.phpfreaks.com/topic/194547-words-not-in-list/#findComment-1023745 Share on other sites More sharing options...
sonnieboy Posted March 9, 2010 Author Share Posted March 9, 2010 xnowandtheworldx, Thank you very, very much. We are almost there. When I added the actual text that we use here at work, then some works that contain some of those are still showing. Take as an example the letter 'a'. If you add the letter a into the array $frags, then that means that the word apple should not appear on the list because it contains the word 'a' but it still appears on that list. I really appreciate your help. Link to comment https://forums.phpfreaks.com/topic/194547-words-not-in-list/#findComment-1023785 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.