如何从 PHP 中的 URL 中删除 http://、www 和斜杠?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9364242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:39:48  来源:igfitidea点击:

How to remove http://, www and slash from URL in PHP?

phpurl

提问by JohnUS

I need a php function which produce a pure domain name from URL. So this function must be remove http://, wwwand /(slash) parts from URL if these parts exists. Here is example input and outputs: Input - > http://www.google.com/| Output -> google.com
Input - > http://google.com/| Output -> google.com
Input - > www.google.com/ | Output -> google.com
Input - > google.com/ | Output -> google.com
Input - > google.com | Output -> google.com

I checked parse_urlfunction, but doesn't return what I need. Since, I'm beginner in PHP, it was difficult for me. If you have any idea, please answer.
Thanx in advance.

我需要一个从 URL 生成纯域名的 php 函数。因此http://,如果这些部分存在,则此功能必须从 URL 中删除www/(斜杠)部分。这是示例输入和输出:输入 - > http://www.google.com/| 输出 -> google.com
输入 -> http://google.com/| 输出 -> google.com
输入 -> www.google.com/ | 输出 -> google.com
输入 -> google.com/ | 输出-> google.com
输入-> google.com | 输出 -> google.com

我检查了parse_url函数,但没有返回我需要的。因为,我是 PHP 初学者,这对我来说很困难。如果您有任何想法,请回答。
提前谢谢。

回答by webbiedave

$input = 'www.google.co.uk/';

// in case scheme relative URI is passed, e.g., //www.google.com/
$input = trim($input, '/');

// If scheme not included, prepend it
if (!preg_match('#^http(s)?://#', $input)) {
    $input = 'http://' . $input;
}

$urlParts = parse_url($input);

// remove www
$domain = preg_replace('/^www\./', '', $urlParts['host']);

echo $domain;

// output: google.co.uk

Works correctly with all your example inputs.

适用于所有示例输入。

回答by Mahdi

$str = 'http://www.google.com/';
$str = preg_replace('#^https?://#', '', rtrim($str,'/'));
echo $str; // www.google.com

回答by Jake

There are lots of ways grab the domain out of a url I've posted 4 ways below starting from the shortest to the longest.

有很多方法可以从 url 中获取域,我在下面发布了 4 种方法,从最短到最长。

#1

#1

function urlToDomain($url) {
   return implode(array_slice(explode('/', preg_replace('/https?:\/\/(www\.)?/', '', $url)), 0, 1));
}
echo urlToDomain('http://www.example.com/directory/index.php?query=true');

#2

#2

function urlToDomain($url) {
   $domain = explode('/', preg_replace('/https?:\/\/(www\.)?/', '', $url));
   return $domain['0'];
}
echo urlToDomain('http://www.example.com/directory/index.php?query=true');

#3

#3

function urlToDomain($url) {
   $domain = preg_replace('/https?:\/\/(www\.)?/', '', $url);
   if ( strpos($domain, '/') !== false ) {
      $explode = explode('/', $domain);
      $domain  = $explode['0'];
   }
   return $domain;
}
echo urlToDomain('http://www.example.com/directory/index.php?query=true');

#4

#4

function urlToDomain($url) {
   if ( substr($url, 0, 8) == 'https://' ) {
      $url = substr($url, 8);
   }
   if ( substr($url, 0, 7) == 'http://' ) {
      $url = substr($url, 7);
   }
   if ( substr($url, 0, 4) == 'www.' ) {
      $url = substr($url, 4);
   }
   if ( strpos($url, '/') !== false ) {
      $explode = explode('/', $url);
      $url     = $explode['0'];
   }
   return $url;
}
echo urlToDomain('http://www.example.com/directory/index.php?query=true');

All of the functions above return the same response: example.com

以上所有函数都返回相同的响应: example.com

回答by h00ligan

Try this, it will remove what you wanted (http:://, www and trailing slash) but will retain other subdomains such as example.google.com

试试这个,它会删除你想要的(http:://、www 和尾部斜杠)但会保留其他子域,例如 example.google.com

$host = parse_url('http://www.google.com', PHP_URL_HOST);
$host = preg_replace('/^(www\.)/i', '', $host);

回答by gintas

if (!preg_match('/^http(s)?:\/\//', $url))
    $url = 'http://' . $url;

$host = parse_url($url, PHP_URL_HOST);
$host = explode('.', strrev($host));
$host = strrev($host[1]) . '.' strrev($host[0]);

This would return second level domain, though it would be useless for say .co.uk domains, so you might want to do some more checking, and include additional parts if strrev($host[0]) is uk, au, etc.

这将返回二级域,尽管它对于 .co.uk 域来说是无用的,因此您可能需要进行更多检查,如果 strrev($host[0]) 是 uk、au 等,则包括其他部分。

回答by stardust4891

$value = 'https://google.ca';
$result = str_ireplace('www.', '', parse_url($value, PHP_URL_HOST));
// google.ca

回答by A. Dady

this will account for http/https and www and the ending slash

这将解释 http/https 和 www 以及结尾的斜杠

$str = 'https://www.google.com/';
$str = preg_replace('#(^https?:\/\/(w{3}\.)?)|(\/$)#', '', $str);
echo $str; // www.google.com

Just ask if you need help understanding the regex.

只需询问您是否需要帮助理解正则表达式。