php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #16040 nested preg_replace_callback()'s won't work
Submitted: 2002-03-13 07:31 UTC Modified: 2002-06-16 01:00 UTC
Votes:3
Avg. Score:5.0 ± 0.0
Reproduced:3 of 3 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: markus dot pfefferle at web dot de Assigned:
Status: No Feedback Package: PCRE related
PHP Version: 4.1.1 OS: Linux
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: markus dot pfefferle at web dot de
New email:
PHP Version: OS:

 

 [2002-03-13 07:31 UTC] markus dot pfefferle at web dot de
A preg_replace_callback() that calls a callback function which in turn makes use of a preg_replace_callback() function too, will not work correctly. The first preg_replace_callback() will substitute gibberisch even though the callback function returned a valid string.

Example:

function funcB($arr)
{
  ...
  return $whatever;
}

function funcA($arr)
{
  ...
  $t = preg_replace_callback("/%value([0-9]+)%/", "funcB", $someotherhaystack)
  return $t; // $t still makes sense here
}

echo preg_replace_callback("/%text([0-9]+)%/", "funcA", $haystack);

// but whatever funcA returned, just some gibberisch is substituted for /%text[0-9]+%/

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-04-09 17:00 UTC] jimw@php.net
the code below works fine for me. please provide a complete example of code that fails.

function funcB($arr) {
  return 'whatever';
}
function funcA($arr) {
  $someotherhaystack = '%value1% foo %value2%';
  $t = preg_replace_callback("/%value([0-9]+)%/", "funcB", $someotherhaystack);
  return $t; // $t still makes sense here
}

$haystack = '%text12% bar %text13%';
echo preg_replace_callback("/%text([0-9]+)%/", "funcA", $haystack);
 [2002-06-16 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2008-12-18 19:44 UTC] ilatypov at infradead dot org
This also holds true for yesterday's CVS snapshot of PHP6, in Windows XP.

I tried installing MediaWiki with the PHP6 snapshot and found that the table prefix replacement in includes/db/Database.php:replaceVars() returns corrupt data sporadically.

I found that pref_replace_callback() was the source of the corruption.

This statement:

        $ins = preg_replace_callback( '/\/\*(?:\$wgDBprefix|_)\*\/([a-zA-Z_0-9]*)/',
            array( &$this, 'tableNameCallback' ),
            $ins );

called the tableName() method through the callback tableNameCallback().  The former method returned a proper string but the end result in $ins was corrupted.

On finding this earlier report, I looked closer at the Database.tableName() method and found a call to preg_match() there.
 [2008-12-18 19:46 UTC] ilatypov at infradead dot org
I downgraded to PHP 5.2.8, and the nested regexp replacement worked.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Jan 03 01:01:29 2025 UTC