如何在php中从三级域或子域查找域扩展
我想从给定域中查找域扩展如何在php中从三级域或子域查找域扩展,php,string,pattern-matching,Php,String,Pattern Matching,我想从给定域中查找域扩展 $domains=array('rni.nic.in','ccert.edu.in', 'xyz.rni.nic.in', 'www.subdomain.gov.in'); 我需要倒数第二个域的值,比如.gov.in或.nic。在中,您可以使用正则表达式匹配点后的字符组 正则表达式: `/\.(\w+)\.(\w+)$/` 对于返回的结果,您可以使用 `preg_match(pattern, subject, match)` 您可以这样做: $domains =
$domains=array('rni.nic.in','ccert.edu.in', 'xyz.rni.nic.in', 'www.subdomain.gov.in');
我需要倒数第二个域的值,比如.gov.in或.nic。在中,您可以使用正则表达式匹配点后的字符组 正则表达式:
`/\.(\w+)\.(\w+)$/`
对于返回的结果,您可以使用
`preg_match(pattern, subject, match)`
您可以这样做:
$domains = array('rni.nic.in', 'ccert.edu.in', 'xyz.rni.nic.in', 'www.subdomain.gov.in');
preg_match('/\.(\w+)\.(\w+)$/', 'rni.nic.in', $domain1);
echo $domain1[0];
preg_match('/\.(\w+)\.(\w+)$/', 'www.subdomain.gov.in', $last);
echo $last[0];
请参阅此处的文档:
==>
希望这能满足您的需要。您能分享您的尝试吗?$allow_domains=array('.gov.in'、'.nic.in'、'.ac.in'、'.edu.in');函数splitDomain($url){$host=”“;$url=parse_url($url);print_r($url);if(isset($url['host']){$host=$url['host'];}else{$host=$url['path'];}$host=str_replace('www.',''''$host);$tmp=explode(',$host);$name=$tmp[0];return$tld=$tmp[1];}$echo$ext=splitDomain('bizdir.gov.in')$l7=substr($ext,-7)$l6=substr($ext,-6);如果(在_数组($l6,$allow_domains)|在_数组($l7,$allow_domains)){echo'got';}编辑您的原始帖子以添加任何新信息或代码。没有办法破译注释中的代码。