|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41580 preg_replace returns NULL, not the unmodified string
Submitted: 2007-06-04 07:01 UTC Modified: 2007-06-04 08:34 UTC
From: steponas at idamas dot net Assigned:
Status: Not a bug Package: PCRE related
PHP Version: 5.2.3 OS: All
Private report: No CVE-ID: None
 [2007-06-04 07:01 UTC] steponas at idamas dot net
the problem is, on this specific string (which I managed to write by accident) preg_replace returns NULL, not the unmodified string (because the regexp isn't matched).

The funny thing is, if I remove some chars from the end of the string, or if I remove all "[quote]" ocurences from the search string, or if I provide a closing tag for the quote ([/quote]) - it works ok (returns the original string). And this bug (as I see it) is found only in PHP 5.2.*.

My current sysinfo:
Windows XP sp2
PHP 5.2.3 as Apache2.2 cli (from binary .zip)
PCRE Library Version 	7.0 18-Dec-2006

PHP versions I tested it on:
5.2.3 - bad
5.2.1 - bad
5.2.0 - bad
5.1.6 - ok, returns the string
4.3.10 - ok
4.3.2 - ok
It works the same on linux and windows.

Reproduce code:
<?php header("Content-type: text/html; charset=utf-8"); ?>
//base64_encoded, because it's UTF-8, Lithuanian
$string = 
echo "The string is:\n".$string."\n\n";

echo "PHP: ".phpversion()."\n";

$regexp = "/\[quote\]((\s|.)*)\[\/quote\]/i";
echo (preg_match($regexp, $string) == true ? "Match found\n" : "Match not found\n").
	"var_dump( preg_replace(...) ): ";
var_dump( preg_replace($regexp, "<span style='background_color: yellow;'>\$1</span>", $string) );

Expected result:
var_dump( preg_replace(...) ): <The original string>

Actual result:
var_dump( preg_replace(...) ): NULL


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2007-06-04 08:34 UTC]
Increase the value of pcre.backtrack_limit.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Wed Dec 08 13:03:41 2021 UTC