Entry
How do I get the file extension of a url: http://cyberparadise.org/example.php (return php)
Oct 26th, 2002 16:43
g b, Ben Udall, Henrik Hansen, Yvo SChaap, http://www.php.net/manual/function.parse-url.php http://www.php.net/manual/en/ref.regex.php
First, you can use the parse_url function to get the path.
$url = 'http://cyberparadise.org/example.php';
$url_parts = parse_url($url);
In this case, $url_parts['path'] would now equal "/example.php". Next,
you can use a regular expression to extract the file extension.
$ext = ereg_replace("^.+\\.([^.]+)$", "\\1", $url_parts['path']);
Alternatively, you can use string functions, as follows:
$ext = substr(strrchr($url, "."), 1)