php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45642 parse problem on preg_replace
Submitted: 2008-07-27 21:00 UTC Modified: 2008-07-28 01:06 UTC
From: zionwilson at gmail dot com Assigned:
Status: Not a bug Package: PCRE related
PHP Version: 5.2.6 OS: Windows Vista x64 Ultimate
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: zionwilson at gmail dot com
New email:
PHP Version: OS:

 

 [2008-07-27 21:00 UTC] 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 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-07-27 23:43 UTC] 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-28 01:06 UTC] zionwilson at gmail dot com
thanks felipe, you're really helpful
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 15 01:01:35 2025 UTC