redarrow Posted June 28, 2007 Share Posted June 28, 2007 match words wont match why? <?php $x="see you soon redarrow"; $word=array("a","see","c"); foreach($word as $bad_word){ if($bad_word==$x){ echo"You entred a bad word!"; exit; }else{ echo "Word is good!"; exit; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/57501-solved-match-words-wont-match-why/ Share on other sites More sharing options...
no_one Posted June 28, 2007 Share Posted June 28, 2007 ??? wouldn't you have to check the $x string using some strstr or similar function? Quote Link to comment https://forums.phpfreaks.com/topic/57501-solved-match-words-wont-match-why/#findComment-284494 Share on other sites More sharing options...
pocobueno1388 Posted June 28, 2007 Share Posted June 28, 2007 <?php $x="see you soon redarrow"; $words=array("a","see","c"); $word = explode(" ", $x); for($i = 0; $i < count($word); $i++){ if (in_array($word[$i], $words)){ echo "$word[$i] - Bad word!<br>"; } else { echo "$word[$i] - Good Word!<br>"; } } ?> EDIT: It was because you were trying to match words to your whole string of words. You had to split up your string then check against every individual word... Quote Link to comment https://forums.phpfreaks.com/topic/57501-solved-match-words-wont-match-why/#findComment-284500 Share on other sites More sharing options...
redarrow Posted June 28, 2007 Author Share Posted June 28, 2007 cheers Quote Link to comment https://forums.phpfreaks.com/topic/57501-solved-match-words-wont-match-why/#findComment-284512 Share on other sites More sharing options...
redarrow Posted June 30, 2007 Author Share Posted June 30, 2007 can u kindly post a proper example to cheek if a word exist in a message please the below last posted example driven me mad cheers. there must ba a simple way to see if a bad word exists in a set condition. <?php $message="your all s and all have you "; $bad_words=array("c","b","s"); $x=explode(" ",$message); for($i=0; $i<count($x); $i++){ $t=in_array($x[$i], $bad_words); if($t==true){ echo "bad word"; }else{ echo "good word"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/57501-solved-match-words-wont-match-why/#findComment-286511 Share on other sites More sharing options...
redarrow Posted June 30, 2007 Author Share Posted June 30, 2007 example the loop wont work <?php $x="you soon see redarrow"; $word=array("a","see","c"); $message=explode(" ",$x); for($i=0; $i < count ($message); $i++){ if(in_array($message[$i], $word)){ echo"You entred a bad word!"; exit; }else{ echo "Word is good!"; exit; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/57501-solved-match-words-wont-match-why/#findComment-286519 Share on other sites More sharing options...
suma237 Posted June 30, 2007 Share Posted June 30, 2007 try this <? $x=" you soon redarrow"; $word=array("a","see","c"); $message=explode(" ",$x); for($i=0; $i < count ($message); $i++){ if(in_array($message[$i], $word)){ echo"You entred a bad word!"; exit; }else{ echo "Word is good!"; exit; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/57501-solved-match-words-wont-match-why/#findComment-286524 Share on other sites More sharing options...
redarrow Posted June 30, 2007 Author Share Posted June 30, 2007 nope i added see to the message no luck <?php $x=" you soon see redarrow"; $word=array("a","see","c"); $message=explode(" ",$x); for($i=0; $i < count ($message); $i++){ if(in_array($message[$i], $word)){ echo"You entred a bad word!"; exit; }else{ echo "Word is good!"; exit; } } ?> tried this aswell <?php $x="you soon see redarrow"; $word=array("a","see","c"); $message=explode(" ",$x); for($i=0; $i < count ($message); $i++){ if(strchr($message[$i],$word)){ echo"You entred a bad word!"; }else{ echo "Word is good!"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/57501-solved-match-words-wont-match-why/#findComment-286527 Share on other sites More sharing options...
redarrow Posted June 30, 2007 Author Share Posted June 30, 2007 this wont work iver <?php $x="you soon see redarrow"; $word=array("a","see","c"); $message=explode(" ",$x); foreach($message as $d){ foreach($word as $c){ if (strstr($d, $c)){ echo"You entred a bad word!"; exit; }else{ echo "Word is good!"; exit; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/57501-solved-match-words-wont-match-why/#findComment-286546 Share on other sites More sharing options...
redarrow Posted June 30, 2007 Author Share Posted June 30, 2007 bump sorry anyone cheers. Quote Link to comment https://forums.phpfreaks.com/topic/57501-solved-match-words-wont-match-why/#findComment-286612 Share on other sites More sharing options...
sasa Posted June 30, 2007 Share Posted June 30, 2007 try <?php function bad_word($string, $array_of_bad_words, $bad_words_find = array()) { $out = true; $bad_words_find = array(); $bad_words_find = array_intersect(explode(' ',$string),$array_of_bad_words); if (count($bad_words_find)) return true; else return false; } $x="you soon see redarrow c c"; $word=array("a","see","c"); if (bad_word($x,$word)) echo "You entred a bad word!"; else echo "Word is good!"; // or echo '<hr />'; $a = array(); bad_word($x, $word, &$a); echo 'Bad words is: ',implode(', ', $a); ?> Quote Link to comment https://forums.phpfreaks.com/topic/57501-solved-match-words-wont-match-why/#findComment-286622 Share on other sites More sharing options...
wildteen88 Posted June 30, 2007 Share Posted June 30, 2007 No need to change your code completely. The code snippet you provided in your first post is nearly there. try this: <?php $str = "see you soon redarrow"; $words = explode(' ', $str); $bad_words = array("a", "see", "c"); foreach($words as $word) { if(in_array($word, $bad_words)) { echo "'$word' - is a bad word!<br />"; } else { echo "'$word' - is a good word!<br />"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/57501-solved-match-words-wont-match-why/#findComment-286633 Share on other sites More sharing options...
redarrow Posted June 30, 2007 Author Share Posted June 30, 2007 wont work like this why please <?php $str = "you see redarrow"; $words = explode(' ', $str); $bad_words = array("a", "see", "c"); foreach($words as $word) { if(in_array($word, $bad_words)) { echo "bad word"; exit; } else { echo "good word"; exit; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/57501-solved-match-words-wont-match-why/#findComment-286641 Share on other sites More sharing options...
wildteen88 Posted June 30, 2007 Share Posted June 30, 2007 becuase you are using the exit keyword in your if/else statement Once a match or non maych has been found you are telling PHP to stop and so nothing else happens. and thus you get Good word. Quote Link to comment https://forums.phpfreaks.com/topic/57501-solved-match-words-wont-match-why/#findComment-286647 Share on other sites More sharing options...
redarrow Posted June 30, 2007 Author Share Posted June 30, 2007 so if it a good word then update database else dont so i need the exit but how and thank u mate. Quote Link to comment https://forums.phpfreaks.com/topic/57501-solved-match-words-wont-match-why/#findComment-286650 Share on other sites More sharing options...
wildteen88 Posted June 30, 2007 Share Posted June 30, 2007 Is this what you mean: <?php $str = "see you soon redarrow"; $words = explode(' ', $str); $bad_words = array("a", "see", "c"); $bad_words_used = 0; foreach($words as $word) { // check whether bad words have been used in string // if they are we increment the 'bad_words_used' variable by one each time a bad word is used if(in_array($word, $bad_words)) { $bad_words_used++; } } // check to see if 'bad_words_used' variable is greater than 0 // if its greater than 0 we kill script display an error // if its not greater than 0 we add to database if($bad_words_used > 0) { die('Bad words have been used'); } // else badwords have not been used. else { echo 'No bad words have been used we add to database now'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/57501-solved-match-words-wont-match-why/#findComment-286656 Share on other sites More sharing options...
redarrow Posted June 30, 2007 Author Share Posted June 30, 2007 Thank you so much wildteen it what i wanted all day your so good. I will trie to learn this now properly. if a user submit a bad word then the user is given a echoed message else database get's updated thank you. I trie to pm another user but the pm dosent work can u fix it please wildteen cheers. Quote Link to comment https://forums.phpfreaks.com/topic/57501-solved-match-words-wont-match-why/#findComment-286663 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.