faqts : Computers : Programming : Languages : PHP : Common Problems : Regular Expressions

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

131 of 160 people (82%) answered Yes
Recently 10 of 10 people (100%) answered Yes

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)