2016-02-20 67 views
0
$file = "refinish.php"; 
$folder = rtrim($file, ".php"); 
echo $folder; // refinis 

哪裏結尾hrtrim函數不適用於結尾h字母

我嘗試了一些其他的結尾字母 - 沒關係。

+0

你預期的結果? –

+0

@VadivelS,我當然希望'修補'。如果它位於字符串的開頭或中間,則不想刪除'.php'。 – bonaca

回答

1

rtrim()不會刪除您在第二個參數中指定的字符串,但是該字符串中的所有字符均爲。在你的情況下,包括「h」。

你所需要的就是一個簡單的str_replace()

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

編輯:如果你想確保它只能從$file結束剝去「.PHP」的一部分,你可以去用@下面和賽馬場的建議使用preg_replace()代替:

$folder = preg_replace('/\.php$/', '', $file); 
+0

多麼愚蠢的語言。非常感謝。如果它位於字符串的開頭或中間,我不需要刪除'.php'。 – bonaca

+1

@bonaca - 如果你閱讀文檔,它會告訴你rtrim究竟做了什麼(有例子).....這不是愚蠢的,它正在做它所說的......但是如果你只是有更好的功能想要從文件名[例如[pathinfo](http://www.php.net/manual/en/function.pathinfo.php) –

+0

@MarkBaker)中刪除文件擴展名,是的,我相信文檔會像你說的那樣說,但它應該說一些不同的東西 - 「修剪」意味着「修剪」,不是。感謝你的'pathinfo'鏈接,對我來說很有用。 – bonaca

1

rtrim的第二個參數是不是子刪除,而是一組人物,也成爲 範圍。如果您想確保只刪除尾部.php,則可以使用preg_replace。例如,

preg_replace("/\.php$/", "", "refinish.php") 
0
$file = "refinish.php"; 
$folder = str_replace('.','',rtrim($file, "php")); 
echo $folder; // refinis 
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

0

如何RTRIM()的作品

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

處理從最後一個字符在$文件通過人物向後工作作爲

$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"; 
相關問題