Jump to content

[SOLVED] (logical) Problem with loop.


play_

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.