mapleleaf Posted June 17, 2010 Share Posted June 17, 2010 Can I replace any items in my array with something else? like: $array = "apple, peach"; echo preg_replace("/($array)/i", "lemon"); Quote Link to comment Share on other sites More sharing options...
kratsg Posted June 18, 2010 Share Posted June 18, 2010 http://us.php.net/manual/en/function.array-map.php Something like.. function replace($item){ return preg_replace("/($item)/i","lemon"); } $arr = array("apple","peach"); $new_arr = array_map("replace",$arr); I also believe this could work: $replace = preg_replace("/($value)/i","lemon"); $arr = array("apple","peach"); $new_arr = array_map($replace,$arr); Quote Link to comment Share on other sites More sharing options...
mapleleaf Posted June 18, 2010 Author Share Posted June 18, 2010 I think I was not clear. I want to replace any words from a body of text that are in an array such as array("peach","apple") with lemon. So $text = "hahdahdakhda asdhasdasd apple jdahibbasd peach dlad ect"; thanks Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted June 18, 2010 Share Posted June 18, 2010 Just use str_replace <?php $text = "hahdahdakhda asdhasdasd apple jdahibbasd peach dlad ect"; $newtext = str_replace(array('apple','peach'),'lemon',$text); echo $newtext . "<br>\n"; ?> Ken Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.