#45642 [Bgs]: parse problem on preg_replace
| From: | zionwilson at gmail dot com | Date: | Mon, 28 Jul 2008 01:06:37 +0000 |
| Subject: | #45642 [Bgs]: parse problem on preg_replace | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-127335@lists.php.net to get a copy of this message | ||
ID: 45642
User updated by: zionwilson at gmail dot com
Reported By: zionwilson at gmail dot com
Status: Bogus
Bug Type: PCRE related
Operating System: Windows Vista x64 Ultimate
PHP Version: 5.2.6
New Comment:
thanks felipe, you're really helpful
Previous Comments:
------------------------------------------------------------------------
[2008-07-27 23:43:26] felipe@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
Note:
$cr4 = intval('\\3')*intval('\\2')+intval('\\1');
and others...
This code obviously will be processed before preg_replace. It isn't a
string, but pure PHP code.
It should be like:
$cp4 = "/([1-9][0-9]*)\s([1-9][0-9]*)\s([1-9][0-9]*)/e";
$cr4 = "intval('\\3')*intval('\\2')+intval('\\1')";
Thanks.
------------------------------------------------------------------------
[2008-07-27 21:00:31] zionwilson at gmail dot com
Description:
------------
for the function preg_replace() i guess it does comparison and other
operation before parse the result from '\\3', '\\2', '\\1' when use a
'general' match pattern like ([1-9][0-9]*)
hence inval() won't work because for string '\\3' or '${3}' the result
is always zero.
however 'modifier-like' match pattern like \d, works well.
so what is the fast work around if i need php to parse "\\1" before
doing any operation like inval(), or comparison, at the moment?
Reproduce code:
---------------
<?
$c1 = "2 4 8";
$cp1 = "/(\d)\s(\d)\s(\d)/e";
$cr1 =
"'\\3*\\2+\\1='.(('\\3')*('\\2')+('\\1'))";
$c2 = "2 4 8";
$cp2 = "/([1-9][0-9]*)\s([1-9][0-9]*)\s([1-9][0-9]*)/";
$cr2 =
"'\\3*\\2+\\1='.(('\\3')*('\\2')+('\\1'))";
$c3 = "2 4 8";
$cp3 = "/([1-9][0-9]*)\s([1-9][0-9]*)\s([1-9][0-9]*)/";
$cr3 =
"'\\3*\\2+\\1='.(('\\3')*('\\2')+('\\1'))";
$c4 = "2 4 8";
$cp4 = "/([1-9][0-9]*)\s([1-9][0-9]*)\s([1-9][0-9]*)/";
$cr4 = intval('\\3')*intval('\\2')+intval('\\1');
$c5 = "2 4 8";
$cp5 = "/([1-9][0-9]*)\s([1-9][0-9]*)\s([1-9][0-9]*)/";
$cr5 = ('\\3' == '${3}' ? '${3}' . ' equals to ' .
'\\3' : '${3}' .
'not equals to' . '\\3');
echo (preg_replace($cp1,$cr1,$c1));
echo ("<br>");
echo (preg_replace($cp2,$cr2,$c2));
echo ("<br>");
echo (preg_replace($cp3,$cr3,$c3));
echo ("<br>");
echo (preg_replace($cp4,$cr4,$c4));
echo ("<br>");
echo (preg_replace($cp5,$cr5,$c5));
?>
Expected result:
----------------
8*4+2=34
8*4+2=34
8*4+2=34
34
8equals to8
Actual result:
--------------
8*4+2=34
'8*4+2='.(('8')*('4')+('2'))
'8*4+2='.(('8')*('4')+('2'))
0
8not equals to8
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=45642&edit=1