php.net |  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
 [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:

http://bugs.php.net/bug.php?id=15166

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...

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-10-29 11:37 UTC] derick@php.net
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-2021 The PHP Group
All rights reserved.
Last updated: Mon Dec 06 18:03:34 2021 UTC