我使用下面的代码来获取" google"打印出来
$domain = parse_url('http://google.com', PHP_URL_HOST);
$url = strstr($domain, ".", true);
echo $url;
只要网址与in.google.com不同,就可以正常工作(现在仅打印#34;")。 有解决方法吗?
注意:我需要将所有扩展名移除.com或.co或.anything
答案 0 :(得分:2)
改用regexp。以下脚本都返回" google"在$matches[1]
in
$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"
没有in
$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"
正如您在以下https://regex101.com链接中看到的那样
https://regex101.com/r/iVyzzw/1/:
它有效。总是,在$matches[1]
,您可以找到谷歌。