|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 03 18:00:01 2025 UTC |
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; }consider this to achieve your goal: basename('hello.js', '.js')