我有字符串输出
foldername/subfolder/filename.jpg
如何仅获取foldername/subfolder/
且仅filename.jpg
$filepath = 'foldername/subfolder/filename.jpg';
$folder= (preg_match('~[^A-Za-z0-9_\./\]~', $filepath));
echo 'folder:' .'<br>' .$folder;
$filename =(preg_match('....', $filepath));
echo 'fileneme' .'<br>' .$filename;
输出
folder:
foldername/subfolder/
filename:
filename.jpg
谢谢
答案 0 :(得分:0)
试试这个
$filepath = 'foldername/subfolder/filename.jpg';
// using regex
$parts = preg_split('~/(?=[^/]*$)~', $filepath );
echo " folder::".$parts[0]." AND fileneme:". $parts[1];
<强> DEMO 强>
或使用 pathinfo
$filepath = 'foldername/subfolder/filename.jpg';
$path_parts = pathinfo($filepath);
echo "dirname : ".$path_parts['dirname']."\n";
echo "basename : ".$path_parts['basename']."\n";
echo "extension : ".$path_parts['extension']."\n";
echo "filename : ".$path_parts['filename']."\n";
<强> DEMO 强>