|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2005-03-21 23:03 UTC] thomas-meyer at uni dot de
Description:
------------
There is a problem with PCRE in both PHP-versions (4.3.10 and 5.0.3)
When using preg_replace with:
$b = preg_replace("/(some)thing/", "\\1$replace", $a);
This works fine, as long as $replace does start with any character that is NOT a number.
BUT: if $replace starts with a number (like "1abc") FIRST this string: "\\11abc" will be created.
THEN it will try to replace \\11 by the 11th remembered position instead of replacing \\1 with the 1st one.
This bug could lead to strange/unexpected results and should be fixed soon.
Reproduce code:
---------------
<?php
$temp = '<input value="test">';
$a = 123;
echo "<P>Perl</P>\n";
$temp1 = preg_replace("/(<[^>]*)test([^>]*>)/Usi", "\\1".$a."\\2", $temp);
echo $temp1;
echo "<P>POSIX</P>\n";
$temp1 = eregi_replace("(<[^>]*)test([^>]*>)", "\\1".$a."\\2", $temp);
echo $temp1;
?>
Expected result:
----------------
<P>Perl</P>
<input value="123">
<P>POSIX</P>
<input value="123">
Actual result:
--------------
<P>Perl</P>
23">
<P>POSIX</P>
<input value="123">
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Dec 03 17:00:01 2025 UTC |
RTFM: "..backreference is immediately followed by another number you cannot use the familiar \\1 notation.. ..the solution is to use \${1}1. This creates an isolated $1 backreference, leaving the 1 as a literal." This works as you expect: echo preg_replace("/(<[^>]*)test([^>]*>)/i", "\${1}{$a}\${2}", $temp);