Re: Help with String Manipulation
| From: | John W. Holmes | Date: | Mon, 26 Apr 2004 19:22:11 +0000 |
| Subject: | Re: Help with String Manipulation | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-184688@lists.php.net to get a copy of this message | ||
From: "Nick Wilson" <nick@stylesheet.org>
> 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?
I assume you mean you only want the "example.com" part, right?
For examples like this, you just just match everything after the first
period (use strpos() to locate first period, then substr() to pull the
rest).
The problem you're going to run into is if a host is passed without a
"subdomain", i.e. just as "example.com". You can guess that since only one
period is passed, the subdomain doesn't exist, but what happens when you get
a domain such as example.com.uk? or www.example.com.uk?
I think you're going to have to set up a list of rules instead of a simple
regex.
1. If beginning is "www.", then strip it and match the rest.
2. If there's only one period, do nothing.
etc... Maybe if there are more than two periods and no "www.", then you'll
have to flag it for human intervention. It depends upon your audience and
where these "host" values are coming from.
---John Holmes...