I have an array:
array('id' => 'really')
I have a string:
$string = 'This should be {id} simple.';
I want to end up with:
This should be really simple.
I have a regular expression that will work with the {id} aspect, but I am having a hard time doing what I want.
/{([a-zA-Z\_\-]*?)}/i
{id} could be anything, {foo} or {bar} or anything that matches my regular expression.
I am sure that there is a simple solution that is escaping me at the moment.
Thanks,
Justin
str_replace is faster then preg_replace, try this:
$arr = array('a' => 'a', 'b' => 'b');
$str = "{a} {b} {c}";
$values = array_values($arr);
$keys = array_keys($arr);
foreach ($keys as $k => $v) {
$keys[$k] = '{'.$v.'}';
}
str_replace($keys, $values, $str);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With