On Tue, 17 Jul 2007 21:28:00 +0200, Ciaran <cr*******@hotmail.comwrote:
Hi can someone give me hand with this please?
What's the best way to extract the extension from the url?
example:
$string="http://www.domain.co.uk/anypage.html"
In this example, I'd be looking for: "co.uk" but it could be "com",
"net" , or any other extension
Why? And there is no easy way to allow for 'double extentions' like co.uk
whithout you giving it a list of accepted doubles. There's no 'logical'
way to allow for this.
Well, to get the TLD:
$string ="http://www.domain.co.uk/anypage.html";
//NON REGEX WAY:
$urlinfo = parse_url($string);
$domaincomponents = explode('.',$urlinfo['host']);
$extention = end($domaincomponents);
//REGEX
preg_match('%
^ #match at start
(?:[a-z]+://)? #possible protocol
[^/]*? #domainstring
([^/.]+) #TLD
(?:/|$) #start of path or end of string
%six',$string,$match);
$extention = $match[1];
If you want to allow for doubles, you'll have to provide a list of
acceptable 'doubles', and match it to the end of the hostname.
--
Rik Wasmus