php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #10668 preg_replace backquote failure
Submitted: 2001-05-04 11:44 UTC Modified: 2001-08-03 12:00 UTC
From: smoonen at andstuff dot org Assigned:
Status: Closed Package: PCRE related
PHP Version: 4.0.5 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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: smoonen at andstuff dot org
New email:
PHP Version: OS:

 

 [2001-05-04 11:44 UTC] smoonen at andstuff dot org
The following code succeeds on PHP 4.03 and PHP 4.04pl1, but fails on PHP 4.05:

  $str = "abc'\\\\''def";
  function f($s) { return "x"; }
  print preg_replace("/c(.*)d/e", "f('\\1')", $str, -1);

This seems to expose *two* underlying bugs:

  1) There appears to be some problem in the regex state
     machine
  2) There is a definite problem with the replacement of
     the backreference with its corresponding string.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-05-04 12:58 UTC] andrei@php.net
Can you explain why you think it fails? The following sample (slightly modified from yours to dump the $s parameter to function):

$str = "abc'\\\\''def";
function f($s) { var_dump($s); return "x"; }
print preg_replace("/c(.*)d/e", "f('\\1')", $str);

outputs:

string(5) "'\\''"
abxef

As it should.
 [2001-05-04 13:59 UTC] smoonen at andstuff dot org
Oddly, I get the following error:

  Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/groups/t/ta/tavi/htdocs/playground/test.php(4) : regexp code on line 1

  Parse error: parse error in /home/groups/t/ta/tavi/htdocs/playground/test.php(4) : regexp code on line 1

  Fatal error: Failed evaluating code: f('\'\\\\'\'') in /home/groups/t/ta/tavi/htdocs/playground/test.php on line 4

PHP compile options may be seen at:

  http://tavi.sourceforge.net/playground/phpinfo.php

 [2001-05-04 15:17 UTC] andrei@php.net
Ok, I made a little mistake, use "f('\\\\1')" rather than "f('\1')".
 [2001-05-04 15:17 UTC] andrei@php.net
Oops, \\1 instead of \1.
 [2001-05-04 15:32 UTC] smoonen at andstuff dot org
H'm...  I'm confused. :-)  To complicate things further, the e-mails display half as many backslashes as the web page. :-)  Since I'm not sure what you mean, I'll exhaustively list the behaviors:

The following:

  $str = "abc'\\\\''def";
  function f($s) { var_dump($s); return "x"; }
  print preg_replace("/c(.*)d/e", "f('\\1')", $str);

gives the error messages I posted earlier.  (That's two backslashes before the 1.)

The following:

  $str = "abc'\\\\''def";
  function f($s) { var_dump($s); return "x"; }
  print preg_replace("/c(.*)d/e", "f('\\\\1')", $str);

gives the result:

  string(2) "\1" abxef

which isn't what I want.  (That's four backslashes before the 1.)

Prefixing the "1" with one or three backslashes in each case emits an ASCII x'01'.

None of which are what I want to occur...


 [2001-08-03 12:00 UTC] smoonen at andstuff dot org
Appears to have been a passing bug in a particular compile.  Cannot repeat.  Closing.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 13:01:29 2024 UTC