|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40961 problem with preg_replace and preg_match functions
Submitted: 2007-03-30 17:30 UTC Modified: 2007-04-18 01:00 UTC
Avg. Score:4.1 ± 0.8
Reproduced:7 of 7 (100.0%)
Same Version:3 (42.9%)
Same OS:2 (28.6%)
From: jfgingras at cegep-ste-foy dot qc dot ca Assigned:
Status: No Feedback Package: PCRE related
PHP Version: 5.2.1 OS: FreeBSD 6.1-RELEASE
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2007-03-30 17:30 UTC] jfgingras at cegep-ste-foy dot qc dot ca
I can't use full PCRE in preg_replace and preg_match functions.

If I do:

$body = "b2c3d4e5f6a7b8c9e0f1a2b3c4d5e6f7";
$body = preg_replace('/(^[a-f0-9]{32}$)/', '?', $body );

preg_replace() return en empty string instead of "?" or the original string if the regex doesn't match as the documentation says.

But, if I use this:

$body = "b2c3d4e5f6a7b8c9e0f1a2b3c4d5e6f7";
$body = preg_replace('^[a-f0-9]{32}$', '?', $body );

It works!!

I got the same problem with preg_match, it always return false.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2007-03-30 21:06 UTC] smlerman at gmail dot com
$body = "b2c3d4e5f6a7b8c9e0f1a2b3c4d5e6f7";
$body = preg_replace('/(^[a-f0-9]{32}$)/', '?', $body );
string(1) "?"

That's what I get in 5.1.6 on Linux and 5.2.1 on Windows.
 [2007-03-30 22:33 UTC] tijnema at gmail dot com
$body = "b2c3d4e5f6a7b8c9e0f1a2b3c4d5e6f7";
$body = preg_replace('/(^[a-f0-9]{32}$)/', '?', $body );

Above code gives me string(1) "?" on PHP-5.1.6/5.2.1/6.0-dev on linux using CLI or Apache.
 [2007-04-02 15:45 UTC] jfgingras at cegep-ste-foy dot qc dot ca
$body = "b2c3d4e5f6a7b8c9e0f1a2b3c4d5e6f7";
$body = preg_replace('/(^[a-f0-9]{32}$)/', '?', $body );

It is not even returning a empty string, it return NULL!

Here's what I have in my php.ini

error_reporting  =  E_ALL
log_errors = On
error_log = /var/log/phperror.log

Here's my php info

[root@cassetout ~]# php -v
PHP 5.2.1 with Suhosin-Patch (cli) (built: Mar 30 2007 10:03:24)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies

Any help is more than welcome.

 [2007-04-02 15:53 UTC] jfgingras at cegep-ste-foy dot qc dot ca
Forget to mention that no php error are logged in the log file. I even set 666 mode on phperror.log just to be sure that php can write in it. But still no error shown.

Strangly, like I said preg_replace works if I remove the '/(' and ')/' from the pattern, but preg_match always return false. But, it print an error:

[02-Apr-2007 11:51:29] PHP Warning:  preg_match() [<a href='function.preg-match'>function.preg-match</a>]: No ending delimiter '^' found in /usr/local/www/f.php on line 6

And here's the line 6:

if(preg_match('^[a-f0-9]{32}$', $body)) echo "YEAH #2!";
 [2007-04-02 21:27 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

Your regex is wrong, you cannot include the "$" (end of string) inside a 
capturing sub-pattern.
 [2007-04-03 14:16 UTC] jfgingras at cegep-ste-foy dot qc dot ca
You're probably right. But still, if I use the following pattern "/([^\x09\x0A\x0D\x20-\x7E\xA0-\xFF])/", preg_replace always return NULL. But if I use "[^\x09\x0A\x0D\x20-\x7E\xA0-\xFF]" it "works", that is, it will not return NULL.

If I use '/^[a-f\d]{32}$/i' on "a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6", preg_match always return false.

The exemples above occur on FreeBSD 6.1 with PHP 5.2.1.
I test thoses on Linux 2.6.9 with PHP 5.1.6 and it work.

Any idea ?
 [2007-04-10 14:20 UTC]
Cannot reproduce both on Linux and FreeBSD.

 [2007-04-10 14:21 UTC]
also check if you are using the bundled pcre library or the system's library. (you can see this in the phpinfo() page).
 [2007-04-10 14:58 UTC] jfgingras at cegep-ste-foy dot qc dot ca
Here's what I have about PCRE with phpinfo() :

PCRE (Perl Compatible Regular Expressions) Support 	enabled
PCRE Library Version 	7.0 18-Dec-2006

Strangly, it's not the first time I have problem with PHP since I upgrade from 5.1.6 to 5.2.1. See bug #40641.
 [2007-04-10 15:54 UTC]
Using the latest snapshot and the code below I get "?" as the result.
$body = "a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6";
$body = preg_replace('/^[a-f\d]{32}$/i', '?', $body );
Tested on Linux and FreeBSD.
 [2007-04-18 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, 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".
PHP Copyright © 2001-2015 The PHP Group
All rights reserved.
Last updated: Mon Mar 02 12:02:27 2015 UTC