redarrow Posted July 30, 2006 Share Posted July 30, 2006 Never used the in_array before but it seems to echo the wrong halted echoed message can you advise me cheers.[code]<?php$words_band=array("red","green","blue","pink","purple");$word="my pink dog keeps barking";if(in_array($word,$words_band)){echo "sorry word not allowed";}else{echo"words are ok";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16017-in_array-help-cheers/ Share on other sites More sharing options...
redarrow Posted July 30, 2006 Author Share Posted July 30, 2006 in_array can only use the word checking for from a sting as a one word but i wanted to no how can you also use the in_array to see multipall words in one string.this is not to get this working in using other php code strictly in_array.[code]<?php$words_band=array("red","green","blue","pink","purple");$word="pink";if(in_array($word,$words_band)){echo "sorry word not allowed";}else{echo"words are ok";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16017-in_array-help-cheers/#findComment-65870 Share on other sites More sharing options...
redarrow Posted July 30, 2006 Author Share Posted July 30, 2006 drivening me mad what wrong with the code please cheers.[code]<?php$words_band=array("red","green","blue","pink","purple");$word="my dog can be a purple ";foreach($words_band AS $key => $value){if(eregi($value,$word)) {echo "sorry but that word is band";exit;}else{echo"word is ok";exit; }}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16017-in_array-help-cheers/#findComment-65891 Share on other sites More sharing options...
wildteen88 Posted July 30, 2006 Share Posted July 30, 2006 You are using exit within your if/else statment. So when PHP first loops through the foreach loop your code will stop. Remove the exit; keywords and you're code and should give you the following result:[code]word is okword is okword is okword is oksorry but that word is band[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16017-in_array-help-cheers/#findComment-66031 Share on other sites More sharing options...
redarrow Posted July 30, 2006 Author Share Posted July 30, 2006 i only wanted this to echo but can not acheve it please help and cheers.becouse the word dickhead is in the $word string it should only show this but like you said it shows both.sorry but that word is band[code]<?php$words_band=array("red","green","blue","pink","purple");$word="my dog can be a purple ";foreach($words_band AS $key => $value){if(eregi($value,$word)) {echo "sorry but that word is band";}else{echo"word is ok"; }}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16017-in_array-help-cheers/#findComment-66061 Share on other sites More sharing options...
hostfreak Posted July 31, 2006 Share Posted July 31, 2006 Try this (thanks to a buddy of mine, Scot from GZE "http://www.gzevolution.net"): [code]<?php$word_filter = array( 'red', 'green', 'blue', 'pink', 'purple',);$post = 'my pink dog keeps barking';function has_bad_words($post){ global $word_filter; $split = preg_split("#\s+#", $post, -1, PREG_SPLIT_NO_EMPTY); if (is_array($split)) { foreach ($split as $post_word) { if (in_array($post_word, $word_filter)) { return true; } } } return false;}if (has_bad_words($post)){ echo "Your post contains bad words, OMG NO.";}else{ echo "Your post is clean, good boy";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16017-in_array-help-cheers/#findComment-66171 Share on other sites More sharing options...
hostfreak Posted July 31, 2006 Share Posted July 31, 2006 Let me know if that was what you were looking for, kind of curious :) Quote Link to comment https://forums.phpfreaks.com/topic/16017-in_array-help-cheers/#findComment-66279 Share on other sites More sharing options...
Ifa Posted July 31, 2006 Share Posted July 31, 2006 Easier way would be[code]$word_filter = array( 'red', 'green', 'blue', 'pink', 'purple',);$post_array = explode(' ', $post);foreach($post_array as $word){if(in_array($word, $word_filter)){echo "sorry but that word is band";$banned = TRUE;break;}}if(!$banned) echo"word is ok";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16017-in_array-help-cheers/#findComment-66290 Share on other sites More sharing options...
zq29 Posted July 31, 2006 Share Posted July 31, 2006 I have just edited out the vulgar words in 6 of the above posts in this thread. Please refrain from using such words in future, they were not necessary to convey your problem. We do have minors browsing the forum, and yes, they probably have heard those words a million times before elsewhere, but thats not the point. Quote Link to comment https://forums.phpfreaks.com/topic/16017-in_array-help-cheers/#findComment-66293 Share on other sites More sharing options...
Ifa Posted July 31, 2006 Share Posted July 31, 2006 sorry :-[ Quote Link to comment https://forums.phpfreaks.com/topic/16017-in_array-help-cheers/#findComment-66297 Share on other sites More sharing options...
hostfreak Posted July 31, 2006 Share Posted July 31, 2006 Sorry, but seems your slacking... lets up our game a bit and get to these problems sooner, can we? I mean, good thing this thread kept getting replies and you finally came to moderate it otherwise I would of been under the impression that using "vulgar" language was ok, or un-moderated. Quote Link to comment https://forums.phpfreaks.com/topic/16017-in_array-help-cheers/#findComment-66329 Share on other sites More sharing options...
Koobi Posted July 31, 2006 Share Posted July 31, 2006 [quote author=hostfreak link=topic=102325.msg406439#msg406439 date=1154352089]Sorry, but seems your slacking... lets up our game a bit and get to these problems sooner, can we? I mean, good thing this thread kept getting replies and you finally came to moderate it otherwise I would of been under the impression that using "vulgar" language was ok, or un-moderated.[/quote]i would have thought that knowing the appropriate place and time to use vulgar language would be common sense :)SA is not slacking. In case you haven't noticed, a whole lot of threads are created everyday.Concerned users generally report a post that they believe seems inappropriate. Feel free to do so yourself in the future :)now let's just get on with this thread. Quote Link to comment https://forums.phpfreaks.com/topic/16017-in_array-help-cheers/#findComment-66332 Share on other sites More sharing options...
wildteen88 Posted July 31, 2006 Share Posted July 31, 2006 SA wasnt slacking, but I was. I should of taken action of changing the words earlier. Quote Link to comment https://forums.phpfreaks.com/topic/16017-in_array-help-cheers/#findComment-66345 Share on other sites More sharing options...
hostfreak Posted July 31, 2006 Share Posted July 31, 2006 After re-reading my post and calming down (I was previously mad about something) I would like to apologize for my post. I hope my apology can be accepted, SemiApocalyptic. No offense was meant, I was mad about something at the time and was just letting anger off. Sorry. Quote Link to comment https://forums.phpfreaks.com/topic/16017-in_array-help-cheers/#findComment-66357 Share on other sites More sharing options...
zq29 Posted July 31, 2006 Share Posted July 31, 2006 No problem what so ever :) Back to the topic at hand... Quote Link to comment https://forums.phpfreaks.com/topic/16017-in_array-help-cheers/#findComment-66378 Share on other sites More sharing options...
hostfreak Posted July 31, 2006 Share Posted July 31, 2006 Just waiting to see if the code I posted is what redarrow was looking for, hopefully he gets on and checks it. Quote Link to comment https://forums.phpfreaks.com/topic/16017-in_array-help-cheers/#findComment-66382 Share on other sites More sharing options...
Ifa Posted July 31, 2006 Share Posted July 31, 2006 [quote author=hostfreak link=topic=102325.msg406495#msg406495 date=1154356856]Just waiting to see if the code I posted is what redarrow was looking for, hopefully he gets on and checks it.[/quote]Well, mine will work if yours doesn't :) Quote Link to comment https://forums.phpfreaks.com/topic/16017-in_array-help-cheers/#findComment-66397 Share on other sites More sharing options...
Koobi Posted July 31, 2006 Share Posted July 31, 2006 great :D now that everyone's back to normal, back to the topic.i decided to throw in a solution of my own:[code=php:0]<?php $words_band=array("red","green","blue","pink","purple"); $word="my pink dog keeps barking"; function filter($data, $banned) { $banned = (array) $banned; foreach($banned as $eachBanned) { if(strpos($data, $eachBanned) === FALSE) { return FALSE; } } return TRUE; } if(filter($word, $words_band) === FALSE) { echo "sorry word not allowed"; } else { echo"words are ok"; }?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16017-in_array-help-cheers/#findComment-66406 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.