Re: Help with String Manipulation

From: Date: Tue, 27 Apr 2004 09:39:40 +0000
Subject: Re: Help with String Manipulation
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-184738@lists.php.net to get a copy of this message
Nick Wilson wrote:
I've never been much good at regex, but it seems there must be a simple way to achieve the following: I have a url that i need to get the 'host' part from for use in email. This almost works: <?php $host=parse_url($base_url); $emailaddy="noreply@".$host['host']; Unfortunately it produces: noreply@www.example.com So, what's the best way to make sure i got ONLY the host part? Many thanks...
The way I've dealt with that in the past is with str_replace() $parsed_url = parse_url($base_url); $email_address = 'noreply@' . str_replace('www.', '', $parsed_url['host']); That'll only give trouble if a domain has a www before the tld, like domainwww.com. Then you could do it like this $email_address = 'noreply@' . (strpos($parsed_url['host'], 'www.') === 0 ? substr($parsed_url['host'], 4) : $parsed_url['host']); Hope it helps. Anguz PS: Tested, it works fine the second way. Here's the script I tried it with: <? $base_url = 'http://www.domainwww.com/dir/file.ext'; $parsed_url = parse_url($base_url); $email_address = 'noreply@' . str_replace('www.', '', $parsed_url['host']); echo $email_address . '<br />'; $email_address = 'noreply@' . (strpos($parsed_url['host'], 'www.') === 0 ? substr($parsed_url['host'], 4) : $parsed_url['host']); echo $email_address; ?>

« previous php.general (#184738) next »