php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #15203 ereg_replace fails on strings containing backslashes
Submitted: 2002-01-24 06:39 UTC Modified: 2002-08-14 14:44 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: josef dot svoboda at email dot cz Assigned:
Status: Not a bug Package: Regexps related
PHP Version: 4.1.2 OS: FreeBSD 4.2
Private report: No CVE-ID: None
 [2002-01-24 06:39 UTC] josef dot svoboda at email dot cz
The goal is to replace sequence of two backslashes in $text by one backslash. While preg_replace works, ereg_replace does not make any change to $text.


echo "preg_replace\n";
$text="\\\\";
echo "--".$text."--".strlen($text)."\n";
$text=preg_replace( "/\\\\\\\\/", "\\\\", $text);
echo "--".$text."--".strlen($text)."\n";
echo "\n";
echo "ereg_replace\n";
$text="\\\\";
echo "--".$text."--".strlen($text)."\n";
$text=ereg_replace( "\\\\\\\\", "\\\\", $text);
echo "--".$text."--".strlen($text)."\n";


./configure \
        --with-mysql=/usr/local/mysql \
        --with-imap=/usr/ports/new/imap \
        --enable-track-vars \
        --enable-trans-sid


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-02-04 02:54 UTC] yohgaki@php.net
The version of PHP that this bug was reported in is too old. Please
try to reproduce this bug in the latest version of PHP (available
from http://www.php.net/downloads.php

If you are still able to reproduce the bug with one of the latest
versions of PHP, please change the PHP version on this bug report
to the version you tested and change the status back to "Open".
 [2002-04-24 09:43 UTC] josef dot svoboda at email dot cz
I tried to replace sequence of two backslashes in $text by one backslash. While preg_replace works, ereg_replace does not make any change to $text.

$text="\\\\"; # this string contains two backslashes
echo "\n".preg_replace( "/\\\\\\\\/", "\\\\", $text); 
# good, prints one backslash
echo "\n".ereg_replace( "\\\\\\\\",   "\\\\", $text);
# bad, prints two backslashes
 [2002-08-14 14:44 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

The correct way to accomplish what you are trying to do would be to use one of the following examples:
<?php
$text="\\\\"; # this string contains two backslashes

echo "\n".preg_replace("!".preg_quote("\\\\")."!", "\\", $text);

echo "\n".ereg_replace(addslashes("\\\\"), "\\", $text);

echo "\n".str_replace("\\\\", "\\", $text);
?>
The latter is the fastest.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 12:01:31 2024 UTC