redarrow Posted August 16, 2006 Share Posted August 16, 2006 foreach wont work why please cheers.[code]<?php$sentence="hi there i think your a word1";$words=array("word1","word2","word3","word4");foreach($words AS $bad){if(eregi($bad,$sentence)){echo " sorry but your useing bad words please try agin but be nice!";exit;}else{echo" your words are lovly";exit;} } ?>[/code][b]edit(shoz): edited for language[/b] Quote Link to comment Share on other sites More sharing options...
otuatail Posted August 16, 2006 Share Posted August 16, 2006 You need to use a flag here otherwise you eill get your words are lovlyyour words are lovlyyour words are lovlysorry but your useing bad words please try agin but be nice!your words are lovlywhich is odd. Quote Link to comment Share on other sites More sharing options...
redarrow Posted August 16, 2006 Author Share Posted August 16, 2006 so what the correct way then becouse i am stuck i tried every way possable cheers. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 16, 2006 Share Posted August 16, 2006 [code]<?php$sentence="hi there i think your a word1";$words=array("word1","word2","word3","word4");foreach($words AS $bad){if(strpos($sentence,$bad)){$badword = 'yes';}}if($badword =='yes'){echo " sorry but your useing bad words please try agin but be nice!";exit;}else{echo" your words are lovly";exit;} ?>[/code]Using strpos because it is the fastest way according to the manual. Quote Link to comment Share on other sites More sharing options...
redarrow Posted August 16, 2006 Author Share Posted August 16, 2006 it works but why dosent my way work dont understand also brake your code down and exsplain it like reading to a child lol.............and thank you cheers mate. Quote Link to comment Share on other sites More sharing options...
shoz Posted August 16, 2006 Share Posted August 16, 2006 redarrow, remember that words generally considered to be offensive should not be posted. Your post has been edited. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 16, 2006 Share Posted August 16, 2006 The problem with your way is that it would always exit the loop on the first iteration.If the first word in your array was in the sentence, it would have shown up as being a bad string, but because the first word in the array wasn't in the sentence, it was saying that the sentence was ok and then exiting.The way i did it, it loops through all of the words and if any of them are in the sentence it simply makes the variable $badwords equal yes.Then after the loop has finished it checks to see if the $badwords does equal yes, and if so it must mean that one of the bad words was found. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 16, 2006 Share Posted August 16, 2006 Ah yes shoz, i fogot to change them on my post too.Also, it would make the script run quicker to add a break; in if a bad word is found because it only needs to know if one of the words is found:[code]<?php$sentence="hi there i think your a word1";$words=array("word1","word2","word3","word4");foreach($words AS $bad){if(strpos($sentence,$bad)){$badword = 'yes';break;//break out of the loop if a bad word is found...continuing is unnecessary}}if($badword =='yes'){echo " sorry but your useing bad words please try agin but be nice!";exit;}else{echo" your words are lovly";exit;} ?>[/code] Quote Link to comment Share on other sites More sharing options...
redarrow Posted August 16, 2006 Author Share Posted August 16, 2006 SORRY SHOZ MATEcheers everyone. 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.