Jump to content


Photo

in_array help cheers.


  • Please log in to reply
17 replies to this topic

#1 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 30 July 2006 - 12:23 PM

Never used the in_array before but it seems to echo the wrong halted echoed message can you advise me cheers.


<?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";

}

?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 30 July 2006 - 12:43 PM

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.

<?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";

}

?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 30 July 2006 - 01:56 PM

drivening me mad what wrong with the code please cheers.




<?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;
 }
}

?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 30 July 2006 - 05:45 PM

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:
word is okword is okword is okword is oksorry but that word is band


#5 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 30 July 2006 - 06:52 PM

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


<?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";

 }
}

?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#6 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 31 July 2006 - 12:19 AM

Try this (thanks to a buddy of mine, Scot from GZE "http://www.gzevolution.net"):

<?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";
}
?>


#7 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 31 July 2006 - 10:10 AM

Let me know if that was what you were looking for, kind of curious  :)

#8 Ifa

Ifa
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationRauma, Finland

Posted 31 July 2006 - 11:44 AM

Easier way would be
$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";

I'm just a guy who likes to code for fun...

#9 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 31 July 2006 - 11:53 AM

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.

#10 Ifa

Ifa
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationRauma, Finland

Posted 31 July 2006 - 11:56 AM

sorry  :-[
I'm just a guy who likes to code for fun...

#11 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 31 July 2006 - 01:21 PM

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.

#12 Koobi

Koobi
  • Staff Alumni
  • Advanced Member
  • 419 posts
  • LocationColombo, Sri Lanka | South Asia

Posted 31 July 2006 - 01:30 PM

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.


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.

#13 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 31 July 2006 - 01:49 PM

SA wasnt slacking, but I was. I should of taken action of changing the words earlier.

#14 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 31 July 2006 - 02:07 PM

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.

#15 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 31 July 2006 - 02:37 PM

No problem what so ever :) Back to the topic at hand...

#16 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 31 July 2006 - 02:40 PM

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

#17 Ifa

Ifa
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationRauma, Finland

Posted 31 July 2006 - 03:13 PM

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


Well, mine will work if yours doesn't :)
I'm just a guy who likes to code for fun...

#18 Koobi

Koobi
  • Staff Alumni
  • Advanced Member
  • 419 posts
  • LocationColombo, Sri Lanka | South Asia

Posted 31 July 2006 - 03:31 PM

great :D now that everyone's back to normal, back to the topic.


i decided to throw in a solution of my own:
<?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";
    }
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users