php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #30468 preg_replace mistake
Submitted: 2004-10-18 09:56 UTC Modified: 2004-10-20 16:37 UTC
From: fn at fkn-systems dot de Assigned:
Status: Not a bug Package: Regexps related
PHP Version: Irrelevant OS: Linux
Private report: No CVE-ID: None
 [2004-10-18 09:56 UTC] fn at fkn-systems dot de
Description:
------------
In:

PHP 4.3.4 (cli) (built: Sep 24 2004 13:01:46)
Copyright (c) 1997-2003 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2003 Zend Technologies
(also in PHP4.1.2 and PHP-CGI)


followed code (whitout newlines!)

$dat='2004-10-17/15:45.15';
echo'_'.preg_replace('/([.^\x5b]*?)(\x5b\x5b.*?\x5d\x5d)(.*)/s','\\1'."$dat".'\\3','# foo 1bar [changed: [[tic:tac]]] foo 2bar #').'_';

gives me

_# foo 1bar [changed: 004-10-17/15:45.15] foo 2bar #_

and not

_# foo 1bar [changed: 2004-10-17/15:45.15] foo 2bar #_



Reproduce code:
---------------
<?php
$dat='2004-10-17/15:45.15';

// (whitout newlines!)
echo'_'.preg_replace('/([.^\x5b]*?)(\x5b\x5b.*?\x5d\x5d)(.*)/s','\\1'."$dat".'\\3','# foo 1bar [changed: [[tic:tac]]] foo 2bar #').'_';
?>

Expected result:
----------------
_# foo 1bar [changed: 2004-10-17/15:45.15] foo 2bar #_


Actual result:
--------------
_# foo 1bar [changed: 004-10-17/15:45.15] foo 2bar #_


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-10-20 16:02 UTC] fn at fkn-systems dot de
OBSOLETE: Was USER-ERROR:
Sorry im very,very blind! :-(
--
doku:preg_replace.html
 When working with a replacement pattern where a backreference is immediately followed by another number (i.e.: placing a literal number immediately after a matched pattern), you cannot use the familiar \\1  notation for your backreference. \\11, for example, would confuse preg_replace() since it does not know whether you want the \\1 backreference followed by a literal 1, or the \\11 backreference followed by nothing. In this case the solution is to use \${1}1. This creates an isolated $1 backreference, leaving the 1  as a literal.
 [2004-10-20 16:37 UTC] derick@php.net
User error -> bogus
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 23:01:29 2024 UTC