php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #30347 ereg_replace() removes the last capital letter
Submitted: 2004-10-07 00:12 UTC Modified: 2004-10-07 01:44 UTC
From: porost at gazeta dot pl Assigned:
Status: Not a bug Package: *Regular Expressions
PHP Version: 4.3.9 OS: Windows XP SP1, Apache/2.0.48
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: porost at gazeta dot pl
New email:
PHP Version: OS:

 

 [2004-10-07 00:12 UTC] porost at gazeta dot pl
Description:
------------
I've found out that some combinations of special characters in regular expressions cause the ereg_replace to cut the last letter in the string, but only if it is a capital letter.
Here is the code:

<?php
 echo ereg_replace('[\+-\.,]$','','MTM') . "<br>";
 echo ereg_replace('[\+-\.,]$','','mtm') . "<br>";
 echo ereg_replace('[\+-\.,]$','','MTm') . "<br>";
 echo ereg_replace('[\+-\.,]$','','mtM') . "<br>";
?>

IMHO one should see:

MTM
mtm
MTm
mtM

but in 2 cases, the last (capital) letter is cut out and you see:

MT
mtm
MTm
mt

There is an easy fix for this problem - you just have to change the regular expression from '[\+-\.,]' to '[-\.\+,]$' and voila - everything is OK.


Reproduce code:
---------------
<?php
 echo ereg_replace('[-\.\+,]$','','MTM') . "<br>";
 echo ereg_replace('[-\.\+,]$','','mtm') . "<br>";
 echo ereg_replace('[-\.\+,]$','','MTm') . "<br>";
 echo ereg_replace('[-\.\+,]$','','mtM') . "<br>";

 echo ereg_replace('[\+-\.,]$','','MTM') . "<br>";
 echo ereg_replace('[\+-\.,]$','','mtm') . "<br>";
 echo ereg_replace('[\+-\.,]$','','MTm') . "<br>";
 echo ereg_replace('[\+-\.,]$','','mtM') . "<br>";
?>

Expected result:
----------------
MTM
mtm
MTm
mtM
MTM
mtm
MTm
mtM

Actual result:
--------------
MTM
mtm
MTm
mtM
MT
mtm
MTm
mt

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-10-07 01:11 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

Read documentation.
 [2004-10-07 01:44 UTC] porost at gazeta dot pl
I must have been looking at this code with the back-side of my head not to see the "-" in the regexp. I'm ashamed :(.

The broken regexp code has been taken from one of the open source CMS systems (Typo3 - it is eating the last capital letter when using its search function) so at least I'm only 99% responsible for posting this stupid mistake ;). Once again - sorry for taking your time.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 12:01:31 2024 UTC