|
Posted by Rik on 07/17/07 19:59
On Tue, 17 Jul 2007 21:28:00 +0200, Ciaran <cronoklee@hotmail.com> wrote=
:
> Hi can someone give me hand with this please?
>
>
> What's the best way to extract the extension from the url?
>
> example:
> $string=3D"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.u=
k =
whithout you giving it a list of accepted doubles. There's no 'logical' =
=
way to allow for this.
Well, to get the TLD:
$string =3D"http://www.domain.co.uk/anypage.html";
//NON REGEX WAY:
$urlinfo =3D parse_url($string);
$domaincomponents =3D explode('.',$urlinfo['host']);
$extention =3D 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 =3D $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
[Back to original message]
|