gpt4 book ai didi

PHP 从 url 中去除域名

转载 作者:行者123 更新时间:2023-12-05 00:49:58 25 4
gpt4 key购买 nike

我知道网上有很多关于这个主题的信息,但我似乎无法按照我想要的方式弄清楚。

我正在尝试构建一个从 url 中删除域名的函数:

http://blabla.com    blabla
www.blabla.net blabla
http://www.blabla.eu blabla

只需要域名的普通名称。

使用 parse_url 我可以过滤域,但这还不够。我有 3 个函数可以限制域,但我仍然得到一些错误的输出

function prepare_array($domains)
{
$prep_domains = explode("\n", str_replace("\r", "", $domains));
$domain_array = array_map('trim', $prep_domains);

return $domain_array;
}

function test($domain)
{
$domain = explode(".", $domain);
return $domain[1];
}

function strip($url)
{
$url = trim($url);
$url = preg_replace("/^(http:\/\/)*(www.)*/is", "", $url);
$url = preg_replace("/\/.*$/is" , "" ,$url);
return $url;
}

允许所有可能的域、url 和扩展名。函数完成后,它必须返回一个仅包含域名本身的数组。

更新:谢谢大家的建议!

在大家的帮助下我想通了。

function test($url) 
{
// Check if the url begins with http:// www. or both
// If so, replace it
if (preg_match("/^(http:\/\/|www.)/i", $url))
{
$domain = preg_replace("/^(http:\/\/)*(www.)*/is", "", $url);
}
else
{
$domain = $url;
}

// Now all thats left is the domain and the extension
// Only return the needed first part without the extension
$domain = explode(".", $domain);

return $domain[0];
}

最佳答案

怎么样

$wsArray = explode(".",$domain); //Break it up into an array. 
$extension = array_pop($wsArray); //Get the Extension (last entry)
$domain = array_pop($wsArray); // Get the domain

http://php.net/manual/en/function.array-pop.php

关于PHP 从 url 中去除域名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32638259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com