Jump to content

Archived

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

redarrow

in_array help cheers.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
sorry  :-[

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites
SA wasnt slacking, but I was. I should of taken action of changing the words earlier.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
No problem what so ever :) Back to the topic at hand...

Share this post


Link to post
Share on other sites
Just waiting to see if the code I posted is what redarrow was looking for, hopefully he gets on and checks it.

Share this post


Link to post
Share on other sites
[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 :)

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

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.