开发者

PHP something faster than explode to get filename from URL

开发者 https://www.devze.com 2022-12-30 05:52 出处:网络
My URL\'s can be absolute or relative: $rel = \"date开发者_Go百科/album/001.jpg\"; $abs = \"http://www.site.com/date/album/image.jpg\";

My URL's can be absolute or relative:

$rel = "date开发者_Go百科/album/001.jpg";
$abs = "http://www.site.com/date/album/image.jpg";

function getFilename($url) {
    $imgName = explode("/", $url);
    $imgName = $imgName[count($imgName) - 1];
    echo $imgName;
}

There must be a faster way to do this right? Maybe a reg expression? But that's Chinese to me..


basename returns the file name:

function getFilename($url) {
    return basename($url);
}

You can even strip the file name extension.


substr( $url , strrpos( $url , "/" ) + 1 );

or

substr( strrchr( $url , "/" ) , 1 );


I believe basename is the quickest one, but you can also use

$url = "http://www.mmrahman.co.uk/image/bg830293.jpg";
getFileName($url){
    $parts = pathinfo($url);
    return $parts['basename'];
}

pathinfo also allow you to get filename, extension, dirname etc.


Try the dirname function:

http://www.php.net/manual/en/function.dirname.php


parse_url()

0

精彩评论

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