php - regex主机名提取 [英] php - regex hostname extraction

查看:165
本文介绍了php - regex主机名提取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一个正则表达式,只会吐出stackoverflow部分,而不是www。或.com等。我可以找到的是检查url是否有效...我已经使用php的url过滤器来确定我现在正在寻找确定哪个网站。



我以前从来没有写过表达,所以我希望有人可以检查一下/推荐一个更好的解决方案。

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆