需要一个正则表达式(使用 PHP)来匹配域名。例子:
http://sub.domain.com/somefolder/index.html -> domain.com
somedomain.info -> somedomain.info
http://anotherdomain.org/home -> anotherdomain.org
www.subdomain.anothersubdomain.maindomain.com/something/ -> maindomain.com
这可能不是正则表达式的工作,而是您选择语言的现有工具的工作。正则表达式并不是一根魔杖,你可以挥舞它来解决所有涉及字符串的问题。您可能想要使用已经编写、测试和调试的现有代码。
在 PHP 中,使用parse_url http://php.net/manual/en/function.parse-url.php功能。
Perl: URI module http://search.cpan.org/dist/URI/.
Ruby: URI module http://www.ruby-doc.org/stdlib-1.9.3/libdoc/uri/rdoc/URI.html.
.NET: ‘Uri’类 http://msdn.microsoft.com/en-us/library/txt7706a.aspx
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)