TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-25-2007, 12:59 AM   #1 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default Domain Keyword

I want to fetch the keyword from the domain.

Currently, my code works with www.domain.com but doesn't work with www.domain.co.uk and www.domain.com.uk or any domain that ends with two extensions.

PHP Code:
$szPattern '/^(http\:\/\/)?(www.)?(.*)\.(.*)$/'
Current Output with www.domain.com

Array ( [0] => http://www.domain.com [1] => http:// [2] => www. [3] => domain [4] => com )

Wrong output with www.domain.com.uk

Array ( [0] => http://www.domain.com.uk [1] => http:// [2] => www. [3] => domain.com [4] => uk )

The .com part should go with the other extension (uk) .
Haris is offline  
Reply With Quote
Old 11-25-2007, 01:28 AM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

How about parse_url instead? Will that do the trick?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-25-2007, 01:37 AM   #3 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
How about parse_url instead? Will that do the trick?
No, Adam. That won't help.
Haris is offline  
Reply With Quote
Old 11-25-2007, 02:00 AM   #4 (permalink)
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default

Well, what you could do.. is just use explode. Make sure the domain is formatted like: www.domain.com.uk or whatever (without slashes or beginning http://), and then just do this:
PHP Code:
/* First get rid of http:// and trailing slash */
$szDomain 'www.domain.co.uk';
$szKeyword explode('.'$szDomain);
echo 
$szKeyword[2]; 
The only error I see this having, if they enter something like: www.sub.domain.com for example, but even then, they shouldn't have the www in front, so it would still work.
Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew is offline  
Reply With Quote
Old 11-25-2007, 02:03 AM   #5 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

Quote:
Originally Posted by Andrew View Post
Well, what you could do.. is just use explode. Make sure the domain is formatted like: www.domain.com.uk or whatever (without slashes or beginning http://), and then just do this:
PHP Code:
/* First get rid of http:// and trailing slash */
$szDomain 'www.domain.co.uk';
$szKeyword explode('.'$szDomain);
echo 
$szKeyword[2]; 
The only error I see this having, if they enter something like: www.sub.domain.com for example, but even then, they shouldn't have the www in front, so it would still work.
Erm, I will try but I would prefer a more cleaner version. I'm heading for sleep and might think good solutions in the morning. ;)

Thanks, though.
Haris is offline  
Reply With Quote
Old 11-25-2007, 02:12 AM   #6 (permalink)
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default

That is a rather clean version.. also, if it works, why not use it if you can't find another way to do it?
Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew is offline  
Reply With Quote
Old 11-25-2007, 03:32 AM   #7 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I'd be tempted to use Andrew's anyway regardless. Regex isn't the fastest, as you'd expect, and if something can be achieved with increased performance, then I would be inclined to go for it.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-25-2007, 09:07 PM   #8 (permalink)
The Acquainted
 
wGEric's Avatar
 
Join Date: Nov 2007
Posts: 166
Thanks: 0
wGEric is on a distinguished road
Default

php Code:
$szPattern = '/^(http\:\/\/)?([url]www.)?([/url][a-z0-9-\.]*)\.([a-z]{0,3})(\.[a-z]{0,2})?$/i';
Try that. It's probably buggy. You should be more strict in what you are allowing for each section. Because the domain name doesn't allow every character. The TLD doesn't allow every character and is usually only a certain length.
__________________
Eric
wGEric is offline  
Reply With Quote
Old 11-26-2007, 05:13 AM   #9 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

Mhmm,

PHP Code:
        $szURL parse_url($szString);
        
$szDomain $szURL['path'];
        
$szKeyword explode('.'$szDomain);
        echo 
$szKeyword[1]; 

Last edited by Haris : 11-27-2007 at 11:34 AM.
Haris is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 04:52 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design