Jump to content

Archived

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

redarrow

foreach dosent work why.

Recommended Posts

foreach wont work why please cheers.

[code]
<?php

$sentence="hi there i think your a word1";

$words=array("word1","word2","word3","word4");

foreach($words AS $bad){

if(eregi($bad,$sentence)){

echo " sorry but your useing bad words please try agin but be nice!";
exit;

}else{

echo" your words are lovly";
exit;
}
}

?>
[/code]

[b]edit(shoz): edited for language[/b]

Share this post


Link to post
Share on other sites
You need to use a flag here otherwise you eill get
your words are lovly
your words are lovly
your words are lovly
sorry but your useing bad words please try agin but be nice!
your words are lovly

which is odd.

Share this post


Link to post
Share on other sites
so what the correct way then becouse i am stuck i tried every way possable cheers.

Share this post


Link to post
Share on other sites
[code]
<?php

$sentence="hi there i think your a word1";

$words=array("word1","word2","word3","word4");

foreach($words AS $bad){
if(strpos($sentence,$bad)){
$badword = 'yes';
}
}
if($badword =='yes'){
echo " sorry but your useing bad words please try agin but be nice!";
exit;
}else{

echo" your words are lovly";
exit;
}


?>
[/code]
Using strpos because it is the fastest way according to the manual.

Share this post


Link to post
Share on other sites
it works but why dosent my way work dont understand also brake your code down and exsplain it like reading to a child lol.............

and thank you cheers mate.

Share this post


Link to post
Share on other sites
redarrow, remember that words generally considered to be offensive should not be posted. Your post has been edited.

Share this post


Link to post
Share on other sites
The problem with your way is that it would always exit the loop on the first iteration.

If the first word in your array was in the sentence, it would have shown up as being a bad string, but because the first word in the array wasn't in the sentence, it was saying that the sentence was ok and then exiting.

The way i did it, it loops through all of the words and if any of them are in the sentence it simply makes the variable $badwords equal yes.

Then after the loop has finished it checks to see if the $badwords does equal yes, and if so it must mean that one of the bad words was found.

Share this post


Link to post
Share on other sites
Ah yes shoz, i fogot to change them on my post too.

Also, it would make the script run quicker to add a break; in if a bad word is found because it only needs to know if one of the words is found:
[code]
<?php

$sentence="hi there i think your a word1";

$words=array("word1","word2","word3","word4");

foreach($words AS $bad){
if(strpos($sentence,$bad)){
$badword = 'yes';
break;//break out of the loop if a bad word is found...continuing is unnecessary
}
}
if($badword =='yes'){
echo " sorry but your useing bad words please try agin but be nice!";
exit;
}else{

echo" your words are lovly";
exit;
}


?>
[/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.