Created
April 8, 2015 07:01
-
-
Save k1ic/020e5452050ec5951090 to your computer and use it in GitHub Desktop.
check is domain in host list or is subDomain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 判断url中的域名是域名列表中的某项或某项的子域名 | |
* @param string $url [必选] | |
* @return bool | |
*/ | |
public static function isDomainInListOrIsSubDomain($url = '') | |
{ | |
$host = parse_url($url, PHP_URL_HOST); | |
$blackList = array('a.com', 'b.cn', 'c.com.cn'); | |
return in_array($host, $blackList) || self::isSubDomain($host, $blackList); | |
} | |
/** | |
* 判断$host是否为$hostList中某项的子域名 | |
* @param string $host [必选] | |
* @param array $hostList [必选] | |
* @return bool | |
*/ | |
public static function isSubDomain($host = '', $hostList = array()) | |
{ | |
foreach ($hostList as $primaryDomain) | |
{ | |
preg_match('/^[a-zA-Z0-9\.-]+' . $primaryDomain . '/', $host, $matches); | |
if (!empty($matches)) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment