$file = \"refinish.php\";
$folder = rtrim($file, \".php\");
echo $folder; // refinis
where is ending h
?
I tried with some other
How rtrim() works
$file = "finish.php";
$folder = rtrim($file, ".php");
is processed working through the characters in $file from the last character backwards as
$file = "finish.php";
// ^
// Is there a `p` in the list of characters to trim
$folder = rtrim($file, ".php");
// ^
// Yes there is, so remove the `p` from the `$file` string
$file = "finish.ph";
// ^
// Is there a `h` in the list of characters to trim
$folder = rtrim($file, ".php");
// ^
// Yes there is, so remove the `h` from the `$file` string
$file = "finish.p";
// ^
// Is there a `p` in the list of characters to trim
$folder = rtrim($file, ".php");
// ^
// Yes there is, so remove the `p` from the `$file` string
$file = "finish.";
// ^
// Is there a `.` in the list of characters to trim
$folder = rtrim($file, ".php");
// ^
// Yes there is, so remove the `.` from the `$file` string
$file = "finish";
// ^
// Is there a `h` in the list of characters to trim
$folder = rtrim($file, ".php");
// ^
// Yes there is, so remove the `h` from the `$file` string
$file = "finis";
// ^
// Is there a `s` in the list of characters to trim
$folder = rtrim($file, ".php");
// ????
// No there isn't, so terminate the checks and return the current value of `$file`
$file = "finis";
$file = "refinish.php";
$folder = str_replace('.','',rtrim($file, "php"));
echo $folder; // refinis
rtrim
's second argument is not the substring to remove but a set of characters—could also be a range. You can use preg_replace
if you want to make sure only the trailing .php
is removed. e.g.,
preg_replace("/\.php$/", "", "refinish.php")
rtrim()
does not remove the string you specify in the second argument, but all characters in that string. In your case, that includes "h".
What you need here is a simple str_replace()
:
$folder = str_replace('.php', '', $file);
Edit: if you want to make sure it strips the ".php" part only from the end of the $file
, you can go with @racetrack's suggestion below and use preg_replace()
instead:
$folder = preg_replace('/\.php$/', '', $file);
You Try code
$filename = "refinish.php";
$extension_pos = strrpos($filename , '.');
$file = substr($filename, 0, $extension_pos) ;
$folder = str_replace('.','',rtrim( $file , "php"));
echo $folder.substr($filename, $extension_pos);
its working fine . its output is refinis.php