gergy008 Posted December 21, 2010 Share Posted December 21, 2010 Dead simple, I know it is but I'm still a little newbie. I have this chat box, and I don't want people to be able to swear in it so I need help making a function where you call the string as an argument and it returns true if the string has any words from an array in it, Even if the word is walking and not just walk. If it's true I can replace the entire string with something else. Can someone tell me what functions I need? I prefer you didn't do it all for me I need to learn Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/222339-match-words-from-an-array-to-a-string/ Share on other sites More sharing options...
BlueSkyIS Posted December 21, 2010 Share Posted December 21, 2010 make the array. loop over the array, seeing whether each word is within the string. you can use strpos, stristr() or other functions to see if one string is within another one. Link to comment https://forums.phpfreaks.com/topic/222339-match-words-from-an-array-to-a-string/#findComment-1150092 Share on other sites More sharing options...
gergy008 Posted December 21, 2010 Author Share Posted December 21, 2010 I made this: $text = $_POST['text']; $words=array('naughty words go here'); $match=false; foreach($words as $word){ $test=strpos($text, $word); if($test==true){ $match=true; } } But yet $match remains false, So it isn't replacing the string... Does anyone know why it won't trigger? Link to comment https://forums.phpfreaks.com/topic/222339-match-words-from-an-array-to-a-string/#findComment-1150101 Share on other sites More sharing options...
BlueSkyIS Posted December 21, 2010 Share Posted December 21, 2010 $words=array('naughty', 'words', 'go', 'here'); Link to comment https://forums.phpfreaks.com/topic/222339-match-words-from-an-array-to-a-string/#findComment-1150104 Share on other sites More sharing options...
gergy008 Posted December 21, 2010 Author Share Posted December 21, 2010 $words=array('naughty', 'words', 'go', 'here'); Thats what I did, I seperated the naughty words like that yet it's still not triggering. $words=array('*', '*', '*', '*', '*', '*', 'etc.'); Link to comment https://forums.phpfreaks.com/topic/222339-match-words-from-an-array-to-a-string/#findComment-1150105 Share on other sites More sharing options...
gergy008 Posted December 22, 2010 Author Share Posted December 22, 2010 Solved: Human error. It was seeing if $matched was false not $match. Link to comment https://forums.phpfreaks.com/topic/222339-match-words-from-an-array-to-a-string/#findComment-1150109 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.