$ar=array("a","b","c");
$string="The third letter of the alpabet is %%2%% and the second is
%%1%%."
$string = preg_replace('/[%]{2}(.*?)[%]{2}/', '$ar[$1]', $string);
echo $string;
I want to see: "The third letter of the alphabet is c and the second
is b."
But what I see instead is: "The third letter of the alphabet is
$ar[$1] and the second is $ar[$1]." I tried double quotes, I tried no
quotes.
The php.net page for preg_replace has some examples in the feedback
that SAY to to this, but go try running them yourself and changing the
numbers- all they really doing is printing the first element, every
time. (In the above example they'd be the equivalent of:
$string = preg_replace('/[%]{2}(.*?)[%]{2}/', $ar[0], $string);)
In perl I could do this easy:
$string=~s/[%]{2}(.*?)[%]{2}/$ar{$1}/g;
How would I do this in PHP?