|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #20150 Exception of the parsing engine of variables in preg_replace()
Submitted: 2002-10-29 11:01 UTC Modified: 2002-10-29 11:37 UTC
From: dakota at dir dot bg Assigned:
Status: Not a bug Package: PCRE related
PHP Version: 4.2.1 OS: WinNT 2000
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
50 + 43 = ?
Subscribe to this entry?

 [2002-10-29 11:01 UTC] dakota at dir dot bg
When the reference identificator in replacement parameter is followed by other regular php variable, the variable is parsed, and after that the number of reference is catched.

$str = "Something to be replaced 7";
$rpl = 6;
echo preg_replace("/^(.*?)[0-9]*$/", "\\1$rpl", $str);

When I expect 7 to be replaced with 6, preg_replace() searches for reference \\16. The problem can be resolved with adding some /e stuff, ot just modifying the regexp.

$str = "Something to be replaced 7";
$rpl = 6;
echo preg_replace("/^(.*?)[0-9]*$/e", "'\\1'.\$rpl", $str);

Even, I can use some of the things proposed in:

Ok, but /e makes the whole thing slower...\\1\\99$rpl is a good solution, but what happens when the maximum number of references becomes 999? :-)

So, the behavior of the function is a little strange in this case...because in the second parameter two things has to be parsed. Once php variables, and then regexp references.

In my opinion, this is a bug. Maybe it's difficult to change the whole parsing mechanism of php cause of one exception, but the exception is already there - references can be numbers (like in Perl), $1, $2, etc...


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2002-10-29 11:37 UTC]
Please do not submit the same bug more than once. An existing
bug report already describes this very problem. Even if you feel
that your issue is somewhat different, the resolution is likely
to be the same. Because of this, we hope you add your comments
to the original bug instead.

Thank you for your interest in PHP.

Duplicate of #15166 but adds no new information -> bogus
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Feb 22 10:01:28 2024 UTC