php.net |  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
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ekomhadi at yahoo dot com
New email:
PHP Version: OS:

 

 [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
/*** PHP START***/
// Found: 26-03-2003

$strBuffer=
'
<pre>
----------------------
preg_replace() bug???
----------------------
<LOOP>
[NUMBER]         [MESG]
</LOOP>
----------------------
</pre>
';
$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***/
?>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-03-26 09:55 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

$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: Fri Mar 29 01:01:28 2024 UTC