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 Ciaran on 07/19/07 02:46

On Jul 17, 8:59 pm, Rik <luiheidsgoe...@hotmail.com> wrote:
> On Tue, 17 Jul 2007 21:28:00 +0200, Ciaran <cronok...@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="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

Hey thanks a lot Rik, This helps a lot!

 

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

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