php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #16537 Bad backslash substitution w/ subexpressions
Submitted: 2002-04-10 16:50 UTC Modified: 2002-04-10 17:14 UTC
From: blueroom at digitalmente dot net Assigned:
Status: Closed Package: PCRE related
PHP Version: 4.1.2 OS: Windows (2000)
Private report: No CVE-ID: None
 [2002-04-10 16:50 UTC] blueroom at digitalmente dot net
Actually, this is quite simple.
Take this:

$preg_replace("/.\:\\.*?\\(.*?).gif/i","http://xpto/\\1.img",$string);

Theoretically, this would make a simple find-replace, but PHP throws out a "unmatched parenthesis" error (Compilation failed: unmatched parentheses at offset 12 blablabla...). With the only parentheses in that regexp being the "(.*?)" in the middle, the error is awkward.

After some juggling I eventually found that it seems that in the "\\(.*?)" part of the expression, the backslash before the parenthesis gets replaced with an actual parenthesis BEFORE the double-backslash is replaced with an actual backslash. I tried putting a space before the parenthesis and no error appeared (obviously, the regexp didn't work like that :).

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-04-10 17:14 UTC] mfischer@php.net
'\' and '\\' will both end up as a single '\' in the string which effectively reads \( to the preg parser which means you escape the '(' and so don't use it's function to capture characters for backreferences which renders the following ')' illegal since it tries to close a not opened '('. Therefore, \ -> \ and \\ -> \ so you need \\\ to get \\ in the string so you don't escape the (. Clear ? :)

Providing input data for such reports is essential.
 
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC