preg_replace: avoiding double replacements
| From: | Andre Polykanine | Date: | Mon, 17 May 2010 23:31:36 +0000 |
| Subject: | preg_replace: avoiding double replacements | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-305290@lists.php.net to get a copy of this message | ||
Hello everyone,
Sorry for bothering you again.
Today I met a problem exactly described by a developer in users' notes
that follow the preg_replace description in the manual:
info at gratisrijden dot nl
02-Oct-2009 02:48
if you are using the preg_replace with arrays, the replacements will apply as subject for the
patterns later in the array. This means replaced values can
be replaced again.
Example:
<?php
$text =
'We want to replace BOLD with the <boldtag> and OLDTAG with the <newtag>';
$patterns
= array(
'/BOLD/i',
'/OLDTAG/i');
$replacements
= array(
'<boldtag>',
'<newtag>');
echo preg_replace
($patterns, $replacements, $text);
?>
Output:
We want to replace <b<newtag>> with the <<b<newtag>>tag> and
<newtag> with the <newtag>
Look what happend with BOLD.
Is there any solution to this besides any two-step sophisticated trick
like case changing?
Thanks!
--
With best regards from Ukraine,
Andre
Http://oire.org/ - The Fantasy blogs of Oire
Skype: Francophile; Wlm&MSN: arthaelon @ yandex.ru; Jabber: arthaelon @ jabber.org
Yahoo! messenger: andre.polykanine; ICQ: 191749952
Twitter: http://twitter.com/m_elensule