Bug #78795 [Nab]: Preg_replace does not always replace

From: Date: Fri, 08 Nov 2019 22:47:58 +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-223651@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 User updated by: markem at sim1 dot us 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: 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. :-) Previous Comments: ------------------------------------------------------------------------ [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

« previous php.bugs (#223651) next »