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

}
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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