Re: Split problem...

From: Date: Thu, 20 Jul 2000 16:21:06 +0000
Subject: Re: Split problem...
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-7439@lists.php.net to get a copy of this message
Rob Curts wrote: > > on 7/20/00 10:43 AM, Kasper (Swebase Network) at kasper@swebase.com wrote: > > > > > Is it possible to split > > > > http://www.php.net > > > > so i only get > > > > php.net and not http://www. ??? > > > > So i always just get the two last words??? > > > > > > Med vänlig hälsning > > Kasper Kristiansson > > 042-217785, Fax 0703-831214 > > > > > yes of course: > > Since a "." is a reserved character for regular expressions, you need to > escape it in the split function. > > <? > $v = "http://www.php.net/" > $a = split("\.",$v); > > /* $a[0] will be "http://www" (no dot at the end) */ > ?> I think he wants to get "php.net" out of the end of the hostname. A more powerful approach would be to use preg_match(). This example will get the last two parts out of the hostname from any URL. <? preg_match("/^(.*)([^\.]+\.[^\.]+)(\/.*)?/U", "http://www.php.net/index.html", $matches); echo "I found: ".$matches[1]."\n"; echo "I found: ".$matches[2]."\n"; echo "I found: ".$matches[3]."\n"; ?> results: I found: http://www. I found: php.net I found: /index.html

« previous php.general (#7439) next »