rtrim function doesn't work with ending h letter

前端 未结 5 823
感动是毒
感动是毒 2021-01-26 04:26
$file = \"refinish.php\";
$folder = rtrim($file, \".php\");
echo $folder; // refinis

where is ending h ?

I tried with some other

相关标签:
5条回答
  • 2021-01-26 04:49

    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";
    
    0 讨论(0)
  • 2021-01-26 04:59
    $file = "refinish.php";
    $folder = str_replace('.','',rtrim($file, "php"));
    echo $folder; // refinis
    
    0 讨论(0)
  • 2021-01-26 05:02

    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")
    
    0 讨论(0)
  • 2021-01-26 05:05

    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);
    
    0 讨论(0)
  • 2021-01-26 05:09

    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

    0 讨论(0)
提交回复
热议问题