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"); Link to comment https://forums.phpfreaks.com/topic/205116-preg_replace-with-an-array/ 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); Link to comment https://forums.phpfreaks.com/topic/205116-preg_replace-with-an-array/#findComment-1073689 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 Link to comment https://forums.phpfreaks.com/topic/205116-preg_replace-with-an-array/#findComment-1073759 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 Link to comment https://forums.phpfreaks.com/topic/205116-preg_replace-with-an-array/#findComment-1073763 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.