|  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
 [2012-02-06 13:46 UTC] elie29 at gmail dot com
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


Add a Patch

Pull Requests

Add a Pull Request


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]
consider this to achieve your goal:

basename('hello.js', '.js')
 [2012-02-19 19:29 UTC]
-Status: Open +Status: Not a bug
 [2012-02-19 19:29 UTC]
No bug here. trim() is working as intended and documented.
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Wed Oct 28 04:01:23 2020 UTC