$path = parse_url($url, PHP_URL_PATH);
parse_url works great (and can provide you with other data like host, port, protocol, etc.)
<?php $url = 'http://website.com/files/2012/10/image001.png'; echo 'URL Parts:' . PHP_EOL; var_dump(parse_url($url)); echo PHP_EOL . 'And specific to path:' . PHP_EOL; echo parse_url($url, PHP_URL_PATH);
Result :
URL Parts: array(3) { ["scheme"]=> string(4) "http" ["host"]=> string(11) "website.com" ["path"]=> string(27) "/files/2012/10/image001.png" } And specific to path: /files/2012/10/image001.png
source share