Jump to content


Photo

foreach dosent work why.


  • Please log in to reply
8 replies to this topic

#1 redarrow

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

Posted 16 August 2006 - 10:54 AM

foreach wont work why please cheers.

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

edit(shoz): edited for language
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 otuatail

otuatail
  • Members
  • PipPipPip
  • Advanced Member
  • 961 posts

Posted 16 August 2006 - 11:13 AM

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.

#3 redarrow

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

Posted 16 August 2006 - 11:20 AM

so what the correct way then becouse i am stuck i tried every way possable cheers.
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 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 16 August 2006 - 11:22 AM

<?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;
}
 
 
?>
Using strpos because it is the fastest way according to the manual.

#5 redarrow

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

Posted 16 August 2006 - 11:32 AM

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.
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 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 16 August 2006 - 11:41 AM

redarrow, remember that words generally considered to be offensive should not be posted. Your post has been edited.

#7 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 16 August 2006 - 11:43 AM

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.

#8 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 16 August 2006 - 11:45 AM

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


#9 redarrow

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

Posted 16 August 2006 - 12:04 PM

SORRY SHOZ MATE

cheers everyone.
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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users