例如,我该怎么做 Output.map
从
F:\Program Files\SSH Communications Security\SSH Secure Shell\Output.map
用PHP?
Mar*_*off 427
你在找basename.
PHP手册中的示例:
<?php
$path = "/home/httpd/html/index.php";
$file = basename($path); // $file is set to "index.php"
$file = basename($path, ".php"); // $file is set to "index"
?>
Run Code Online (Sandbox Code Playgroud)
Met*_*iel 63
我已经使用函数PATHINFO创建了这个函数,该函数创建了一个包含路径部分的数组供您使用!例如,您可以这样做:
<?php
$xmlFile = pathinfo('/usr/admin/config/test.xml');
function filePathParts($arg1) {
echo $arg1['dirname'], "\n";
echo $arg1['basename'], "\n";
echo $arg1['extension'], "\n";
echo $arg1['filename'], "\n";
}
filePathParts($xmlFile);
?>
Run Code Online (Sandbox Code Playgroud)
这将返回:
/usr/admin/config
test.xml
xml
test
Run Code Online (Sandbox Code Playgroud)
从PHP 5.2.0开始就可以使用这个函数了!
然后,您可以根据需要操作所有部件.例如,要使用完整路径,您可以执行以下操作:
$fullPath = $xmlFile['dirname'] . '/' . $xmlFile['basename'];
Run Code Online (Sandbox Code Playgroud)
Pas*_*TIN 12
该basename功能应该给你你想要的:
给定包含文件路径的字符串,此函数将返回文件的基本名称.
例如,引用手册的页面:
<?php
$path = "/home/httpd/html/index.php";
$file = basename($path); // $file is set to "index.php"
$file = basename($path, ".php"); // $file is set to "index"
?>
Run Code Online (Sandbox Code Playgroud)
或者,在您的情况下:
$full = 'F:\Program Files\SSH Communications Security\SSH Secure Shell\Output.map';
var_dump(basename($full));
Run Code Online (Sandbox Code Playgroud)
你会得到:
string(10) "Output.map"
Run Code Online (Sandbox Code Playgroud)
使用SplFileInfo:
SplFileInfo SplFileInfo类为单个文件的信息提供高级面向对象的接口.
参考:http://php.net/manual/en/splfileinfo.getfilename.php
$info = new SplFileInfo('/path/to/foo.txt');
var_dump($info->getFilename());
Run Code Online (Sandbox Code Playgroud)
o/p:string(7)"foo.txt"
有几种方法可以获取文件名和扩展名.您可以使用以下易于使用的.
$url = 'http://www.nepaltraveldoor.com/images/trekking/nepal/annapurna-region/Annapurna-region-trekking.jpg';
$file = file_get_contents($url); // To get file
$name = basename($url); // To get file name
$ext = pathinfo($url, PATHINFO_EXTENSION); // To get extension
$name2 =pathinfo($url, PATHINFO_FILENAME); // File name without extension
Run Code Online (Sandbox Code Playgroud)
basename()在处理像中文这样的亚洲字符时有一个bug.
我用这个:
function get_basename($filename)
{
return preg_replace('/^.+[\\\\\\/]/', '', $filename);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
339382 次 |
| 最近记录: |