rtrim函数不能用于结束h字母

时间:2016-02-20 09:37:39

标签: php

$file = "refinish.php";
$folder = rtrim($file, ".php");
echo $folder; // refinis

哪里结束h

我试着用其他一些结尾的字母 - 没关系。

5 个答案:

答案 0 :(得分:1)

rtrim()不会删除您在第二个参数中指定的字符串,但会删除该字符串中的所有字符。在您的情况下,包括“h”。

这里需要的是一个简单的str_replace()

$folder = str_replace('.php', '', $file);

编辑:如果您想确保仅从$file的末尾删除“.php”部分,您可以使用@ racetrack的建议,并使用{{ 1}}代替:

preg_replace()

答案 1 :(得分:1)

rtrim的第二个参数不是要删除的子字符串,而是一组字符 - 也可以是一个范围。如果您想确保仅删除尾随preg_replace,则可以使用.php。如,

preg_replace("/\.php$/", "", "refinish.php")

答案 2 :(得分:0)

$file = "refinish.php";
$folder = str_replace('.','',rtrim($file, "php"));
echo $folder; // refinis

答案 3 :(得分:0)

您尝试代码

$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);

工作正常。它的输出是 refinis.php

答案 4 :(得分:0)

rtrim()如何运作

$file = "finish.php";
$folder = rtrim($file, ".php");
处理

后,将$ file中的字符从最后一个字符向后处理为

$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";