PHP rtrim “.php”

后端 未结 3 2044
情歌与酒
情歌与酒 2021-01-14 00:02

I want to remove \".php\" from the end of a string if it exists. Consider this:

$filename = \'index\';
rtrim($filename,\".php\");//returns \"index\"

$filena         


        
相关标签:
3条回答
  • 2021-01-14 00:20

    if you're simply trying to remove the extension, why not use this: $filename = 'index.php'; $name = strstr($filename, '.', true);

    0 讨论(0)
  • 2021-01-14 00:25

    The second argument to rtrim is a string with a list of characters. In this case, it will strip off any P, H, and . in your string, so returning searc.

    0 讨论(0)
  • 2021-01-14 00:29

    rtrim accepts a list of characters as the second argument, so in this case, it will trim not just the .php extension, but any ., p, or h characters found in the rest of the string.

    Try using preg_replace("/(.+)\.php$/", "$1", $filename); instead, or basename($filename, '.php') if you have the file on the server, not just in a string.

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