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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.