php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78795 Preg_replace does not always replace
Submitted: 2019-11-08 03:01 UTC Modified: 2019-11-09 07:45 UTC
From: markem at sim1 dot us Assigned: cmb (profile)
Status: Not a bug Package: Regexps related
PHP Version: 7.3.11 OS: Windows 10
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: markem at sim1 dot us
New email:
PHP Version: OS:

 

 [2019-11-08 03:01 UTC] markem at sim1 dot us
Description:
------------
---
From manual page: https://php.net/function.preg-replace
---

I was working on correcting a function by dawidgarus at gmail dot com. Got it working (yeah!). But then decided to write my own and expand upon it. That was when I ran into this problem:

If you use preg_replace, the "replace" string can not use the $x or ${x} within another variable (like an array). This generates an error.

Test script:
---------------
function bc()
{
   $argv = func_get_args();

   $cmd = $argv[0];
   $cmd = preg_replace( "/\$(\d+)/", $argv[$1], $cmd );
   return( $cmd );
}

   $a = bc( "sqrt($1*$1+$2*$2)", 3, 5 );

Expected result:
----------------
$a should equal "sqrt(3*3+5*5)"

Actual result:
--------------
Notice: Undefined variable: i in C:\Users\Mark\My Programs\PHP\lib\bcd.php on line ###


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-11-08 08:02 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2019-11-08 08:02 UTC] cmb@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

For a start, see <https://3v4l.org/AsuaR>.
 [2019-11-08 22:47 UTC] markem at sim1 dot us
Hey! Not asking for help with a program - reporting a bug.

I noted that it said "i" instead of "1". Was hoping you guys would understand. But no - so here is another test program. Same problem:

<?php

    $s = $argv[1];
    $s = preg_replace( "/\$(\d+)/", $argv[$1], $s );
    echo "S = $s\n";

?>

Test command:

c:>php test.php "sqrt(($1*$1)+($2*$2))" 3 5

Result:

Parse error: syntax error, unexpected '1' (T_LNUMBER), expecting variable (T_VARIABLE) or '{' or '$' in C:\test.php on line 4

shell returned 255

Please note the '1'. That '1' is being put inside of the "$argv", this should be "$argv[1]" but instead, the '$' in front of the '1' causes PHP to think it is a variable which produces the error.

Better? Sorry for the mis-post the first time. Tried to edit it but that did not work.

By the way: The ORIGINAL source code had "/\$(\d+)/e" which the "e" option is no longer supported in preg_replace. The only way to take care of this is to use the preg_replace_callback() function. BUT! I thought that PHP should be able to use the '$1' and correctly substitute the found value in the replacement section.

ALSO BTW : If you change the replacement area with "$argv[${1}]" - PHP still complains:

Source:

<?php

    $s = $argv[1];
    $s = preg_replace( "/\$(\d+)/", $argv[${1}], $s );
    echo "S = $s\n";

?>

command:

php test.php "sqrt(($1*$1)+($2*$2))" 3 5

Results:

C:\>php test.php "sqrt(($1*$1)+($2*$2))" 3 5

Notice: Undefined variable: 1 in C:\test.php on line 4

Notice: Undefined index:  in C:\test.php on line 4
S = sqrt(($1*$1)+($2*$2))

Sorry for the botched post earlier. Hopefully this one shows you what I mean. :-)
 [2019-11-08 22:55 UTC] nikic@php.net
You are looking for preg_replace_callback.
 [2019-11-08 22:59 UTC] requinix@php.net
Oh don't worry, @cmb knew exactly what you were trying to do. Problem is that what you are trying to do is incorrect.

preg_replace lets you use "$1" in a string to perform a replacement. That has absolutely nothing to do with whether you can use $1 as a variable. You cannot. The fact that they both involve a dollar sign is just coincidence - PCRE could have chosen % or & or any other symbol, and if they did it wouldn't mean you could write $argv[%1] or $argv[&1] in your code.

If you want any sort of dynamic PHP behavior, like "take the number that was matched and find it in the $argv array", then you need preg_replace_callback like @nikic said.
 [2019-11-09 04:48 UTC] markem at sim1 dot us
Yeah, that's what I did. I just thought this was a bug because PHP substitutes in strings otherwise. Also - there isn't anything in the documentation that I have seen talking about this. Might be a good idea to put something in there. (The preg_replace() command.) Maybe pointing to the preg_replace_callback() function. Oh well. Thanks! :-)

You can close this.
 [2019-11-09 07:45 UTC] cmb@php.net
> (The preg_replace() command.) Maybe pointing to the
> preg_replace_callback() function.

The changelog of preg_replace() already states:

| Support for the /e modifier has been removed. Use
| preg_replace_callback() instead.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 12:01:31 2024 UTC