ToonMariner Posted July 25, 2007 Share Posted July 25, 2007 Done this before and can't bloody replicate it... I have a string that contains place holders like so... Hello #name# and welcome to playgroup. You are #age# and have #limbs#. I have an array like so.. <?php $data['name'] = 'George'; $data['age'] = 3; $data['limbs'] = 4; ?> so I want to replace #XX# with the corresponding index in the array... something like... preg_replace('/#(.)*?#/',$data['$1'],$str); but I just can't get it... Please help before my monitor takes a hit. Quote Link to comment Share on other sites More sharing options...
Wildbug Posted July 25, 2007 Share Posted July 25, 2007 You'll need the /e modifier (execute substitution as PHP code). preg_replace('/#(.)*?#/e',"$data[$1]",$str); Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted July 26, 2007 Author Share Posted July 26, 2007 I got it to work - but with the replacement string singled quoted (double quoted failed!) THANK YOU - you are a gentleman and a scholar. Quote Link to comment Share on other sites More sharing options...
DeadEvil Posted July 26, 2007 Share Posted July 26, 2007 try this.. preg_replace('/#([A-Za-z0-9])?#/e',"$data[$1]",$str); 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.