php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60992 Trim / rtrim bug when we need to delete extension
Submitted: 2012-02-06 13:46 UTC Modified: 2012-02-19 19:29 UTC
From: elie29 at gmail dot com Assigned:
Status: Not a bug Package: Strings related
PHP Version: 5.3.10 OS: windows
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: elie29 at gmail dot com
New email:
PHP Version: OS:

 

 [2012-02-06 13:46 UTC] elie29 at gmail dot com
Description:
------------
If we need to delete from a file name its extension using trim or rtrim function, 
the return value is uncorrect when your file name is ended with an 's' characters.

Exemple :
rtrim('users.js', '.js'); => will return user instead of users

Test script:
---------------
var_dump(rtrim('users.js', 'js')); // users. OK
var_dump(rtrim('users.', '.')); //users OK
var_dump(rtrim('users.js', '.js')); //  user instead of users KO
var_dump(trim('users.php', '.php')); // user instead of users KO
var_dump(rtrim('correct.js', '.js')); //correct OK
var_dump(trim('ssss.js', '.js')); // empty string instead of ssss KO

Expected result:
----------------
var_dump(rtrim('users.js', 'js')); // users.
var_dump(rtrim('users.', '.')); //users
var_dump(rtrim('users.js', '.js')); //  users
var_dump(trim('users.php', '.php')); // users
var_dump(rtrim('correct.js', '.js')); //correct
var_dump(trim('ssss.js', '.js')); // sss


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-02-06 14:03 UTC] anon at anon dot anon
This is the correct logic for the trim functions. As stated in the documentation, the second parameter is a list of characters, not a solid string to test for verbatim. Hence `rtrim('users.php', 'ph.'))` is also 'users'. Try something like this instead:

function rtrim_str($str, $fragment) {
	if (substr($str, -strlen($fragment)) === $fragment) $str = substr($str, 0, -strlen($fragment));
	return $str;
}
 [2012-02-19 19:27 UTC] ab@php.net
consider this to achieve your goal:

basename('hello.js', '.js')
 [2012-02-19 19:29 UTC] rasmus@php.net
-Status: Open +Status: Not a bug
 [2012-02-19 19:29 UTC] rasmus@php.net
No bug here. trim() is working as intended and documented.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 11:01:30 2024 UTC