TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Domain Keyword (http://www.talkphp.com/absolute-beginners/1516-domain-keyword.html)

Haris 11-25-2007 12:59 AM

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) .

Wildhoney 11-25-2007 01:28 AM

How about parse_url instead? Will that do the trick?

Haris 11-25-2007 01:37 AM

Quote:

Originally Posted by Wildhoney (Post 4561)
How about parse_url instead? Will that do the trick?

No, Adam. That won't help.

Andrew 11-25-2007 02:00 AM

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.

Haris 11-25-2007 02:03 AM

Quote:

Originally Posted by Andrew (Post 4563)
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.

Andrew 11-25-2007 02:12 AM

That is a rather clean version.. also, if it works, why not use it if you can't find another way to do it?

Wildhoney 11-25-2007 03:32 AM

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.

wGEric 11-25-2007 09:07 PM

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.

Haris 11-26-2007 05:13 AM

Mhmm,

PHP Code:

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



All times are GMT. The time now is 01:24 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0