Bug #78795 [Nab]: Preg_replace does not always replace
| From: | requinix@php.net | Date: | Fri, 08 Nov 2019 22:59:27 +0000 |
| Subject: | Bug #78795 [Nab]: Preg_replace does not always replace | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-223654@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=78795&edit=1
ID: 78795
Updated by: requinix@php.net
Reported by: markem at sim1 dot us
Summary: Preg_replace does not always replace
Status: Not a bug
Type: Bug
Package: Regexps related
Operating System: Windows 10
PHP Version: 7.3.11
Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2019-11-08 22:55:07] nikic@php.net
You are looking for preg_replace_callback.
------------------------------------------------------------------------
[2019-11-08 22:47:58] 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 08:02:19] 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 03:01:12] 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 ###
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=78795&edit=1