php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #34562 Backslash escaping doesn't work
Submitted: 2005-09-20 13:11 UTC Modified: 2005-09-20 13:44 UTC
From: techtonik@php.net Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.4.0 OS: Windows 2000
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: techtonik@php.net
New email:
PHP Version: OS:

 

 [2005-09-20 13:11 UTC] techtonik@php.net
Description:
------------
Usually, if you want to match a backslash, you write "\\", but this doesn't work no matter what kind of quotes do you use.


Reproduce code:
---------------
<?php

$str = 'slash\replace';

echo $str = preg_replace('!\\+!', '/', $str);

preg_replace('![/\\]+!', '/', $str); // warning

?>

Expected result:
----------------
slash/replace

Actual result:
--------------
slash\replace
Warning: preg_replace(): Compilation failed: missing terminating ] for character class at offset 5 i
n C:\MyFiles.XFiles\Numerald\tc\test69\testcase.php on line 7

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-09-20 13:16 UTC] sniper@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.


 [2005-09-20 13:27 UTC] techtonik@php.net
Ok, the problem is what PHP makes additional escaping of backslashes and it is not mentioned in PCRE syntax pages.

http://www.php.net/manual/en/reference.pcre.pattern.syntax.php#regexp.reference.backslash

It must be noted to use '\\\\' for backslash since PHP escapes backslash in strings before making regular expressions from them.


 [2005-09-20 13:44 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

"Single and double quoted PHP strings have special meaning of backslash. Thus if \ has to be matched with a regular expression \\, then "\\\\" or '\\\\' must be used in PHP code."
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon May 27 03:01:30 2024 UTC