tags:

views:

279

answers:

5

My URL's can be absolute or relative:

$rel = "date/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..

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

or

substr( strrchr( $url , "/" ) , 1 );
drawnonward
complicated!!!!
Galen
A: 

Try the dirname function:

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

W_P
+7  A: 

basename returns the file name:

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

You can even strip the file name extension.

Gumbo
excellent! Cheers Gumbo.
FFish
A: 

parse_url

Mark Baker
A: 

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.

mmrs151