在php中,从完整的google url和子域中仅获取“google”

在php中,从完整的google url和子域中仅获取“google”,php,Php,我用下面的代码把谷歌打印出来 $domain = parse_url('http://google.com', PHP_URL_HOST); $url = strstr($domain, ".", true); echo $url; 只要url不是像现在打印的in.google.com一样,就可以正常工作。 有解决办法吗 注意:我需要所有扩展名都是remove.com或.co或.anything。请改用regexp。以下两个脚本都返回google inside$matches[1] 加入 不在

我用下面的代码把谷歌打印出来

$domain = parse_url('http://google.com', PHP_URL_HOST);
$url = strstr($domain, ".", true);
echo $url;
只要url不是像现在打印的in.google.com一样,就可以正常工作。 有解决办法吗


注意:我需要所有扩展名都是remove.com或.co或.anything。请改用regexp。以下两个脚本都返回google inside$matches[1]

加入

不在

正如您在下面的链接中所看到的

:


它起作用了。始终,在$matches[1]中,您可以找到google。

对于in.google.com,您仍然需要google还是in.google?@simon only google(如果可能的话)请查看我的答案^如果URL是images.google.co.uk怎么办?我不确定你要找的东西是否适用于所有情况。这只适用于.com。OP说我需要所有的扩展名都是remove.com或.co或.anything因为我的答案会复制/粘贴你的,所以我只留下一条评论;-像这样的东西可以抓住任何tld,并解决一些最常见的两部分问题:$regexp=/[\w]{0,}\.edu | gov | net | mil | org | co | ac | ed | gv | gob |[a-z0 9]+$/i;。这对于噩梦来说是不够的,但这只是一个开始。@sensorario Hi很抱歉反应太晚了。我正忙着做其他事情,直到现在还不能证实这一点。我检查了这一点,似乎对每一个提交到我的网站超过300网址的工作。可能还有一些我不知道的边缘案例,如果出现任何问题,我会告诉你。到目前为止,这似乎是正确的。刚刚把它集成到网站上。谢谢:
$regexp = "/([\w]{0,}).([\w]{2,3}|[\w]{2,3}.[\w]{2,3})$/";
$input = parse_url('http://in.google.com', PHP_URL_HOST);
preg_match($regexp, $input, $matches);
var_dump($matches[1]); // string(6) "google"
$regexp = "/([\w]{0,}).([\w]{2,3}|[\w]{2,3}.[\w]{2,3})$/";
$input = parse_url('http://google.com', PHP_URL_HOST);
preg_match($regexp, $input, $matches);
var_dump($matches[1]); // string(6) "google"