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 Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
47 + 21 = ?
Subscribe to this entry?

 
 [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

Add a Patch

Pull Requests

Add a Pull Request

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: Fri Apr 26 12:01:30 2024 UTC