play_ Posted October 26, 2009 Share Posted October 26, 2009 Hi. I can't seem to figure this out. It seems so simple....maybe it's because it's 5:30am and I haven't slept yet. Anyhow, In a form I have a "tags" input, where user types tags and separates them by commas. On the php script, i explode at the commas and run a loop. In the loop, i run a function called "filter_tags()" which sees if the word is a bad word(curse word) or not. Here's the filter_tag() function: function filter_tags($tag) { $bad_words = array('bad foo', 'bad bar'); // clean example if( in_array($tag, $bad_words) ) { return true; } } Before inserting tag into database, i check to see if it's a bad word. here's the loop: $tags = explode(',', $tags); foreach( $tags as $value ) { if( filter_tags($value) ) { // this word is in the bad_words array. dirty word echo "bad word: $value <br>"; } else { // clean word. NOT in bad_words array echo "good word: $value <br>"; } } Here's the problem: Say i type in these tags in the form: bad foo, test, whatever That will output: bad word: bad foo good word: test good word: whatever which is the expected output. However, if i type in a non-bad word first, followed by a bad one, like so: hello, bad foo, whatever it outputs good word: hello good word: bad foo <---- this should be 'bad word' good word: whatever I cannot figure out why this is happening. anyone? Link to comment https://forums.phpfreaks.com/topic/179036-solved-logical-problem-with-loop/ Share on other sites More sharing options...
Adam Posted October 26, 2009 Share Posted October 26, 2009 Possibly need to use trim() .. Link to comment https://forums.phpfreaks.com/topic/179036-solved-logical-problem-with-loop/#findComment-944598 Share on other sites More sharing options...
Bendude14 Posted October 26, 2009 Share Posted October 26, 2009 MrAdam is correct add trim into your function, function filter_tags($tag) { $bad_words = array('bad foo', 'bad bar'); // clean example if( in_array(trim($tag), $bad_words) ) { return true; } } Link to comment https://forums.phpfreaks.com/topic/179036-solved-logical-problem-with-loop/#findComment-944604 Share on other sites More sharing options...
play_ Posted October 26, 2009 Author Share Posted October 26, 2009 ah i knew it was something simple. thanks. Link to comment https://forums.phpfreaks.com/topic/179036-solved-logical-problem-with-loop/#findComment-944798 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.