Jump to content

code problam please help cheers


redarrow

Recommended Posts

What am i doing wrong i keep getting the XXX no other

words please exsplain cheers........

 

 

the result should be

 

i said XXX

 

not

 

XXX

 

<?php 

$text="i said another_swear_word";

$a=array("swear_word","another_swear_word","another_another_swear_word");

$text=explode(' ',$text);

foreach($text as $text){

if(in_array("$text",$a)){

$text=eregi_replace("$text","XXX",$text);

echo $text;

}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/96477-code-problam-please-help-cheers/
Share on other sites

I think it's because, in the foreach line, you're using the same variable name ($text) as the array and the variable that each element of the array is assigned to. After the first iteration, you're overwriting the $text array with the first element of the $text array (it's like writing $text = $text[0]). Also, you changed the $text variable into an array of words, and then tried to echo it like a string in the foreach loop. You should probably have a seperate variable for the array. This code should work:

 

<?php 

$text="i said another_swear_word";

$a=array("swear_word","another_swear_word","another_another_swear_word");

$text_array=explode(' ',$text);

foreach($text_array as $word){

if(in_array("$word",$a)){

$text=eregi_replace("$word","XXX",$text);

echo $text;

}
}
?>

Archived

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

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