如何在php中验证正确的域名和子域 [英] How to validate a correct domain name and sub domain in php
本文介绍了如何在php中验证正确的域名和子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何删除 www 和验证有效的域名。
strong>
domain.com
subdomain.domain.com
subdomain.domain.com
域无效
www.domain.com
www.subdomain.domain.com
http://www.domain.com/
http:// www .subdomain.com /
www.domain.com/folder/
如何代码?
$ domain ='www.domain.com / folder /';
if(){
// validate here
}
解决方案
首先,手动删除www。那么只需确保它是在domain.tld或something.domain.tld的形式。
$ domain = str_replace('www。',' ',$ domain);
if(preg_match('/([a-zA-Z0-9\\ \\ -\\],[a-zA-Z0-9\ -_] + \。[a-zA-Z] {2,5} /',$ domain))
//有效域
How to remove www and validate a valid domain name.
Valid Domain
domain.com
subdomain.domain.com
sub-domain.domain.com
Invalid Domain
www.domain.com
www.subdomain.domain.com
http://www.domain.com/
http://www.subdomain.com/
www.domain.com/folder/
How the code?
$domain ='www.domain.com/folder/';
if() {
// validate here
}
解决方案
First, manually strip out the www. then just make sure it is in the form domain.tld or something.domain.tld
$domain = str_replace('www.','',$domain);
if(preg_match('/([a-zA-Z0-9\-_]+\.)?[a-zA-Z0-9\-_]+\.[a-zA-Z]{2,5}/',$domain))
//valid domain
这篇关于如何在php中验证正确的域名和子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文