php.net |  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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: steponas at idamas dot net
New email:
PHP Version: OS:

 

 [2007-06-04 07:01 UTC] steponas at idamas dot net
Description:
------------
Hello,
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"); ?>
<pre>
<?php
//base64_encoded, because it's UTF-8, Lithuanian
$string = 
base64_decode("VcW+ZHJhdXN0YSBmb3J1bXVvc2UgcmHFoXl0aSBjaXRhdMWzIHBha2xvZGVz".
"IC0gdC55LiwgamVpZ3UgcmHFoXlzaXRlIDMtaXItZGF1Z2lhdSBuZQ0KdcW+ZGFyeXRhcyBjaW".
"F0YXRhcyAoW3F1b3RlXS4uLltxdW90ZV0uLi5bcXVvdGVdLi4uKSwgdGFpIGp1bXMgYnVzIGFw".
"aWUgdGFpIHByYW5lxaF0YSBpciDFvmludQ0KdMSXIG5lYnVzIGnFoXNpxbNzdGEgLSBz");
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) );
?>
</pre>

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

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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-06-04 08:34 UTC] tony2001@php.net
Increase the value of pcre.backtrack_limit.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 05:01:28 2024 UTC