开发者

removing last 3 characters on a file (file extension)

my file name are being stored in a variable $file_name... how can i remove the extension and just hav开发者_运维知识库e the name only? is there something other than strcmp that i can use... that doesn't seem to do it


Use pathinfo.

<?php
$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');

echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n"; // since PHP 5.2.0
?>

Note: If you're not on PHP >= 5.2 use this to compose the filename

$path_parts['filename'] = substr($path_parts['basename'], 0, -1*strlen($path_parts['extension'])-1);


You can do:

$file_name_no_ext = pathinfo($file_name , PATHINFO_FILENAME);


substr($file_name, 0, -4);


You could use regular expressions. Regular expression to remove a file's extension


You should take a look at the pathinfo() function.


Similar to @fire, but more robust in the face of multiple dots:

$comps = explode('.', $filename);
unset($comps[count($comps) - 1]);
$ext = implode('.', $comps);


I use:

$file_name = current(explode(".", $file_name));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜