我有几个类似的url,如下:
https://example.com/
http://example.com/
我只想要字符串 "example.com"
并且我想要移除
https:// 和 http://
所以我已经创建了这样的数组,
$removeChar = ["https://", "http://", "/"];
正确的方式是如何移除这些?
我有几个类似的url,如下:
https://example.com/
http://example.com/
我只想要字符串 "example.com"
并且我想要移除
https:// 和 http://
所以我已经创建了这样的数组,
$removeChar = ["https://", "http://", "/"];
正确的方式是如何移除这些?
使用这个php函数。
链接: http://php.net/manual/zh/function.parse-url.php (parse_url php函数)
$url = "http://example.com/";
$domain = parse_url($url, PHP_URL_HOST);
获取结果 example.com
这对我有用,
$http_referer = str_replace($removeChar, "", "https://example.com/");
try this:
$string = url ... ( your url);
$removeChar= array("http://","https://","/");
foreach($char in $removeChar)
{
$string= str_replace($char,"",$string);
}
有内置函数:
$domain = parse_url($url, PHP_URL_HOST);
http://example.com/login?test=1
,那么你只会得到example.com
。请参考我的评论。 - zoubida13$url = "https://www.example.com";
// Remove http:// or https:// from beginning of string
$url = preg_replace('/^(http:\/\/|https:\/\/)/', '', $url);
echo $url; // Output: www.example.com
parse_url()
是否是最佳选项,因为它返回一个具有不可预测键数量的关联数组(即您必须测试是否存在“查询”等),考虑到您只想摆脱方案部分,在这种情况下,您必须要么删除方案部分,然后将所有其他部分拼接在一起成为字符串,我个人会选择简单的str_replace
。 - zoubida13