Ask a Question
Advertise on boostr.in
Organizational Research By
Surprising Reserch Topic
preg replace with array replacements using -'php,regex,string'
preg replace with array replacements ¬†using -'php,regex,string'
$string = ":abc and :def have apples.";
$replacements = array('Mary', 'Jane');
Mary and Jane have apples.
Right now I'm doing it like this:
preg_match_all('/:(\w+)/', $string, $matches);
foreach($matches as $index => $match)
¬†¬†¬†$string = str_replace($match, $replacements[$index], $string);
Can I do this in a single run, using something like preg_replace?
Sep 7, 2015
to add a comment.
Related Hot Questions
Your name to display (optional):
Email me at this address if my answer is selected or commented on:
Email me if my answer is selected or commented on
Privacy: Your email address will only be used for sending these notifications.
To avoid this verification in future, please