从php中的字符串中删除http和https
我有几个网址,比如从php中的字符串中删除http和https,php,arrays,preg-match,Php,Arrays,Preg Match,我有几个网址,比如 我只想要“example.com”作为字符串 我想把它去掉 https://和http:// 所以我采取了这样的阵列 $removeChar = ["https://", "http://", "/"]; 移除这些的正确方法是什么?这对我很有效 $http_referer = str_replace($removeChar, "", "https://example.com/"); 试试这个: $string = url ... ( your url); $r
我只想要“example.com”作为字符串 我想把它去掉 https://和http:// 所以我采取了这样的阵列
$removeChar = ["https://", "http://", "/"];
移除这些的正确方法是什么?这对我很有效
$http_referer = str_replace($removeChar, "", "https://example.com/");
试试这个:
$string = url ... ( your url);
$removeChar= array("http://","https://","/");
foreach($char in $removeChar)
{
$string= str_replace($char,"",$string);
}
有内置功能:
$domain = parse_url($url, PHP_URL_HOST);
使用这个php函数
链接:(解析url php函数)
获取一个结果
example.com
parse_url(),谢谢@splash58。这是一个很大的帮助。@splash58@Keyur对于这个特定的例子,我不确定parse_url()
是否是最好的选择,因为它返回一个包含不可预测数量的键的关联数组(即,您必须测试“query”是否存在,等等),考虑到您只想去掉scheme部分,在这种情况下,您必须删除scheme部分,然后将所有其他部分合并到一个字符串中。我个人会用一个简单的str_replace
@zoubida13然后别忘了删除'port'@splash58这是我的观点,但是如果url是http://example.com/login?test=1
那么您将只获得example.com
。请参阅我在OP上的评论。我相信parse_url如果不是http,将不会返回任何内容。例如:$url=“example.com”;
$url = "http://example.com/";
$domain = parse_url($url, PHP_URL_HOST);