By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,723 Members | 1,891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,723 IT Pros & Developers. It's quick & easy.

Need help? Display domain name

P: 1
Can any one kindly post a solution.

I want to use php to display the domain name typed in by the user. Parse it to display in the desired format. This result will be checked against a database and relevant content is displayed.

Example:

Visitor types in www.yahoo.com. This will be parsed into yahoo.com and then matched against a mysql database entry and then display content accordingly.

I appreciate your help. Thanks.
May 7 '07 #1
Share this Question
Share on Google+
1 Reply


pbmods
Expert 5K+
P: 5,821
Off the top of my head, you could probably do something like this:
[EDIT: Please don't do it off the top of my head; a desk would be much more comfortable for both of us.]

[PHP]
if(preg_match('/([-\w_]+(?:\.\w{2,4})+)(?=(\/|$))/', $_REQUEST['domain'], $matches)) {
var_dump($matches);
...
[/PHP]

This will match any alphanumeric or hyphen/underscore characters, then any series of subdomains (alphanumeric only, 2-4 chars), immediately preceding either the first '/' character, or the end of the string.

This matches stuff like 'yahoo.co.uk', 'army.mil', 'google.com', but admittedly it also matches '------------------------------------.mug', so you might have to do a little extra validation.

[EDIT: Incidentally, $matches has three elements after running this regex on my system, but it should only have one. Apparently, the PCRE engine in PHP 5.2.1 (or at least the one I compiled on my Mac) does not properly support non-capturing parenthesis nor lookaheads properly.]
May 8 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.