php - regex主机名提取 [英] php - regex hostname extraction
本文介绍了php - regex主机名提取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我以前从来没有写过表达,所以我希望有人可以检查一下/推荐一个更好的解决方案。
preg_match('@ ^(?:http://)(?:www。)?([^。] +)@i',$ url,$ matches)
/ pre>
编辑:
我正在处理的所有url都是.com,如果这有帮助解决方案一行不使用正则表达式!!!
$ url ='http ://stackoverflow.com;
$ d = array_shift(explode('。',str_replace('www。','',parse_url($ url,PHP_URL_HOST))));
echo $ d;
I have been looking around for a regex expression that will spit out just the 'stackoverflow' part and no www. or .com etc. All I could find was to check if the url's were valid... I have used php's url filter to determine that much I now am looking to determine which site it is.
I have never written an expression before so I am hoping someone can check it/recommend a better solution.
preg_match('@^(?:http://)(?:www.)?([^.]+)@i', $url, $matches)
edit: All the url's I am dealing with are .com if that helps
解决方案one-liner without using regular expressions!!!
$url = 'http://stackoverflow.com'; $d = array_shift( explode( '.', str_replace('www.', '', parse_url( $url, PHP_URL_HOST )) ) ); echo $d;
这篇关于php - regex主机名提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文