redarrow Posted March 17, 2008 Share Posted March 17, 2008 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 More sharing options...
Jeremysr Posted March 17, 2008 Share Posted March 17, 2008 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 https://forums.phpfreaks.com/topic/96477-code-problam-please-help-cheers/#findComment-493758 Share on other sites More sharing options...
redarrow Posted March 17, 2008 Author Share Posted March 17, 2008 thank you SOLVED... wasent to far out then lol...... Link to comment https://forums.phpfreaks.com/topic/96477-code-problam-please-help-cheers/#findComment-493780 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.