Forum Moderators: coopster

Message Too Old, No Replies

Matching subdomains to domain in array

         

ocon

8:01 pm on Dec 5, 2009 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hello,

I have an array that contains different domain names:

$domain_list=array("domain.edu","website.edu","site.edu","school.ac.uk");

I have a variable $url that I need to treat differently depending on if the domain of $url is in the $domain_list.

I calculate the domain of $url using:

$domain=strtolower(parse_url($url,PHP_URL_HOST));
if(substr($domain,0,4)=="www.") $domain=substr($domain,4);

And detect if the $domain is in the $domain_list using:

if(in_array($domain,$follow_links)){...}
else{...}

That works great, except when the $url links to a subdomain, like:

[mail.domain.edu...]
[students.website.edu...]
[art.site.edu...]

Is there any way to make the script match if the $url is in the array, and not match links to sites like:

[notdomain.edu...]
[mysite.edu...]
[notschool.ac.uk...]
[other.ac.uk...]
[website.edu.junksite.com...]

Thanks

vincevincevince

3:39 am on Dec 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should probably use a preg_match() expression

Somethink like:

if (preg_match("/http\:\/\/([a-z\.]+\.¦)".preg_quote($domain)."/i",$url)) ...

(Change ¦ to unbroken pipe)