|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #22886 preg_replace $0 and $1
Submitted: 2003-03-25 22:23 UTC Modified: 2003-03-26 09:55 UTC
From: ekomhadi at yahoo dot com Assigned:
Status: Not a bug Package: PCRE related
PHP Version: 4.3.0 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
5 + 42 = ?
Subscribe to this entry?

 [2003-03-25 22:23 UTC] ekomhadi at yahoo dot com
I'm using PHP 4.3.0
I found problem when using preg_replace(..)
string $0 and $1 will mess up and not replace correctly
also $N with N is Integer will not appear. If the character after the dollar sign is not a number, it works as expected.

Here is my code:
/*** PHP START***/
// Found: 26-03-2003

preg_replace() bug???
[NUMBER]         [MESG]
$strNumber = 123456789;
$strMesg   = "text with $0 and $1 string";

$Patttern = "/<LOOP>(.*?)<\/LOOP>/is";
preg_match($Patttern, $strBuffer, $arrTag);
$strData  = str_replace("[NUMBER]",   $strNumber, $arrTag[1]);
$strData  = str_replace("[MESG]",     $strMesg,   $strData);
$strBuffer= preg_replace($Patttern, $strData, $strBuffer);
echo $strBuffer;

/*** PHP END***/


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2003-03-26 09:55 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

$0 and $1 are not valid variable names and in case of regex they are treated like $0 (entire matched string) and $1 1st matched element.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Feb 27 00:01:26 2024 UTC