php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #39695 preg_replace returns NULL if no match, instead of unmodified subject
Submitted: 2006-11-30 16:55 UTC Modified: 2006-11-30 21:36 UTC
From: ivo_gelov at abv dot bg Assigned:
Status: Not a bug Package: PCRE related
PHP Version: 5.2.0 OS: Linux Fedora Core 4
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: ivo_gelov at abv dot bg
New email:
PHP Version: OS:

 

 [2006-11-30 16:55 UTC] ivo_gelov at abv dot bg
Description:
------------
There are 2 strange effects/bugs, may be one depends on the other, I do not know.
1. PREG_REPLACE does not recognize $n as a back reference in its second argument. It will recognize only \\n references.
2. PREG_REPLACE returns NULL if no match is found, instead of returning unmodified subject

This symptom is present only in 5.2.0 - there are no problems with 5.1.0

Reproduce code:
---------------
$pat = '/^([a-zA-Z])(\d*)$/';
$rep = '\$2\$1';
$b = preg_replace($pat,$rep,'D764209812');


Expected result:
----------------
$b should be '764209812D'

Actual result:
--------------
$b is NULL

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-11-30 17:36 UTC] tony2001@php.net
Not reproducible.
Btw, this code - $rep = '\$2\$1'; apparently won't work.
 [2006-11-30 18:05 UTC] ivo_gelov at abv dot bg
I checked "$2$1" instead of "\$2\$1" and it worked.
So this is my fault since I was looking an example in an old manual, not the current one.
Sorry for disturbing.

But there is another issue - when I used preg_replace in PHP 5.1.4 with a relatively long string as a subject, it worked without problems. But after upgrade to 5.2.0, it now gives me an error PREG_BACKTRACK_LIMIT_ERROR with default value for "pcre.backtrack_limit" (not listed in PHP.INI at all). What can I do ? Should I revert back to 5.1.4 ?
Or just compile 5.2.0 with the 6.6 version of PCRE library ?
 [2006-11-30 18:17 UTC] tony2001@php.net
Just increase pcre.backtrack_limit.
 [2006-11-30 18:58 UTC] ivo_gelov at abv dot bg
But what is the limit for this setting ?
 [2006-11-30 21:36 UTC] tony2001@php.net
AFAIK both maximum for recursion and match limits is set to 10000000.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 02 10:01:38 2025 UTC