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
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: 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

Pull Requests

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: Mon Dec 30 17:01:29 2024 UTC