You are here: Re: php extract variable from a string using regexp « PHP Programming Language « IT news, forums, messages
Re: php extract variable from a string using regexp

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация