我有完整的URL作为字符串,但我想删除字符串开头的http://以很好地显示URL(例如:www.google.com而不是http://www.google.com)
有人可以帮忙吗?
Sar*_*raz 135
$str = 'http://www.google.com';
$str = preg_replace('#^https?://#', '', $str);
echo $str; // www.google.com
Run Code Online (Sandbox Code Playgroud)
这将为双方的工作http://和https://
Shi*_*dim 23
你根本不需要正则表达式.请改用str_replace.
str_replace('http://', '', $subject);
str_replace('https://', '', $subject);
Run Code Online (Sandbox Code Playgroud)
合并成一个单独的操作如下:
str_replace(array('http://','https://'), '', $urlString);
Run Code Online (Sandbox Code Playgroud)
Ift*_*mud 19
更好地利用这个:
$url = parse_url($url);
$url = $url['host'];
echo $url;
Run Code Online (Sandbox Code Playgroud)
更简单,适用于http:// https:// ftp://几乎所有前缀.