RE: [PHP] preg_replace problem
| From: | Maxim Maletsky | Date: | Fri, 13 Oct 2000 08:42:21 +0000 |
| Subject: | RE: [PHP] preg_replace problem | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-19965@lists.php.net to get a copy of this message | ||
could this work for you?
$string=preg_replace("/\[(\w+)\]/e", $a[1], $string);
Did you try?
-----Original Message-----
From: Nicolae Mihalache [mailto:mache@unibuc.ro]
Sent: Friday, October 13, 2000 4:29 PM
To: php-general@lists.php.net
Subject: [PHP] preg_replace problem
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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
For additional commands, e-mail: php-general-help@lists.php.net
To contact the list administrators, e-mail: php-list-admin@lists.php.net