Re: pulling data out of a string

From: Date: Thu, 24 May 2001 08:53:45 +0000
Subject: Re: pulling data out of a string
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-54183@lists.php.net to get a copy of this message
Previously, Richard Kurth said: > > That my problem www is not always the host when it is a sub-domain it > would be like mysit.domain.net or sometimes people put a few extra > periods in the domain name like www.my.test.net I thought maybe I > could look at the string and find out how many periods are in it then > if there is a www before the first period if not treat it like a > sub-domain. The last period is is any thing after it would be the tld. > How would I count the periods This is a little ugly, maybe, but it works: <?php $hostname = 'www.myhost.test.com'; $prefix = preg_replace( "/\..*$/", "", $hostname); $middle = preg_replace( "/^[^.]+\./", "", $hostname); $middle = preg_replace( "/\.[^.]+$/", "", $middle); $suffix = preg_replace( "/^.*\./", "", $hostname); print "prefix= $prefix<br>"; print "suffix= $suffix<br>"; print "middle= $middle<br>"; ?> Will output: prefix= www middle= myhost.test suffix= com -d -- If God dropped acid, would he see people? -George Carlin

« previous php.general (#54183) next »