preg_replace problem
| From: | Nicolae Mihalache | Date: | Fri, 13 Oct 2000 07:28:36 +0000 |
| Subject: | preg_replace problem | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-19958@lists.php.net to get a copy of this message | ||
Hello,
I'm trying to write a piece of code which replace in
a string every occurrence of "[something]" with $a["something"]
(a is an array resulted from a mysql_fetch_array).
However for some reason this code does not work:
<?php
$a["email"]="test@test.com";
$a["name"]="Foo Bar";
$string="this is a string containing [email] and [name]";
$string=preg_replace("/\[(\w+)\]/e","$a['\\1']",$string);
echo "string=$string";
?>
I tried with and without "e" modifier and a lot of
combination, all without luck. The problem is that php seems
to expand variable before replacing '\\n' which make sense
if I don't use the 'e' modifier but don't make sense if I
use it.
The only thing that seems to work is to write a function
which take a key as argument and return the corresponding
value:
function dumb($key)
{
global $a;
return $a[$key];
}
$string=preg_replace("/\[(\w+)\]/e","dumb('\\1')",$string);
but if I have more than one array, I have to write a function
for every array.
I think there is something I miss here, any help will be
appreciated.
Thank you,
Mache