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

发布于 2025-01-07 21:52:16 字数 519 浏览 0 评论 0原文

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

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

I need a php function which produce a pure domain name from URL. So this function must be remove http://, www and /(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_url function, 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

预谋 2025-01-14 21:52:16
$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

与您的所有示例输入一起正常工作。

$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.

℡Ms空城旧梦 2025-01-14 21:52:16
$str = 'http://www.google.com/';
$str = preg_replace('#^https?://#', '', rtrim($str,'/'));
echo $str; // www.google.com
$str = 'http://www.google.com/';
$str = preg_replace('#^https?://#', '', rtrim($str,'/'));
echo $str; // www.google.com
扭转时空 2025-01-14 21:52:16

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

#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

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

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

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');

上面的所有函数都返回相同的响应:示例.com

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.

#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

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

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

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

诗笺 2025-01-14 21:52:16

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

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

或者作为单行:

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

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

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

Or as a one-liner:

$host = preg_replace('/^(www\.)/i', '', parse_url('http://www.google.com', PHP_URL_HOST));
Smile简单爱 2025-01-14 21:52:16
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]);

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

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.

惟欲睡 2025-01-14 21:52:16

第一种方法是使用一个正则表达式来修剪 URL 中不必要的部分,例如协议、www 和结尾斜杠。

function trimUrlProtocol($url) {
    return preg_replace('/((^https?:\/\/)?(www\.)?)|(\/$)/', '', trim($url));
}

echo trimUrlProtocol('http://sandbox.onlinephpfunctions.com/') . PHP_EOL;
echo trimUrlProtocol('https://sandbox.onlinephpfunctions.com/') . PHP_EOL;
echo trimUrlProtocol('http://www.sandbox.onlinephpfunctions.com/') . PHP_EOL;
echo trimUrlProtocol('https://www.sandbox.onlinephpfunctions.com/') . PHP_EOL;
echo trimUrlProtocol('http://sandbox.onlinephpfunctions.com') . PHP_EOL;
echo trimUrlProtocol('https://sandbox.onlinephpfunctions.com') . PHP_EOL;
echo trimUrlProtocol('http://www.sandbox.onlinephpfunctions.com') . PHP_EOL;
echo trimUrlProtocol('https://www.sandbox.onlinephpfunctions.com') . PHP_EOL;
echo trimUrlProtocol('sandbox.onlinephpfunctions.com') . PHP_EOL;

通过替代方法,您可以使用 parse_url,但是你必须进行额外的检查来检查 host 部分是否存在,然后使用正则表达式修剪 www.就用第一种方法,简单又懒。

First way is to use one regular expression to trim unnecesary parts of URL like protocol, www and ending slash

function trimUrlProtocol($url) {
    return preg_replace('/((^https?:\/\/)?(www\.)?)|(\/$)/', '', trim($url));
}

echo trimUrlProtocol('http://sandbox.onlinephpfunctions.com/') . PHP_EOL;
echo trimUrlProtocol('https://sandbox.onlinephpfunctions.com/') . PHP_EOL;
echo trimUrlProtocol('http://www.sandbox.onlinephpfunctions.com/') . PHP_EOL;
echo trimUrlProtocol('https://www.sandbox.onlinephpfunctions.com/') . PHP_EOL;
echo trimUrlProtocol('http://sandbox.onlinephpfunctions.com') . PHP_EOL;
echo trimUrlProtocol('https://sandbox.onlinephpfunctions.com') . PHP_EOL;
echo trimUrlProtocol('http://www.sandbox.onlinephpfunctions.com') . PHP_EOL;
echo trimUrlProtocol('https://www.sandbox.onlinephpfunctions.com') . PHP_EOL;
echo trimUrlProtocol('sandbox.onlinephpfunctions.com') . PHP_EOL;

By alternative way you can use parse_url, but you have to make additional cheks to check if host part exists and then use regular expression to trim www. Just use first way, it is simple and lazy.

用心笑 2025-01-14 21:52:16
$value = 'https://google.ca';
$result = str_ireplace('www.', '', parse_url($value, PHP_URL_HOST));
// google.ca
$value = 'https://google.ca';
$result = str_ireplace('www.', '', parse_url($value, PHP_URL_HOST));
// google.ca
余生一个溪 2025-01-14 21:52:16

这将解释“http/https”、“www”和结尾斜杠,

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

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

This will account for "http/https", "www" and the ending slash

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

Just ask if you need help understanding the regex.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文