PHP 正则表达式从字符串中删除 http://
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9549866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Regex to Remove http:// from string
提问by Casey
I have full URLs as strings, but I want to remove the http:// at the beginning of the string to display the URL nicely (ex: www.google.com instead of http://www.google.com)
我有完整的 URL 作为字符串,但我想删除字符串开头的 http:// 以很好地显示 URL(例如: www.google.com 而不是http://www.google.com)
Can someone help?
有人可以帮忙吗?
回答by Sarfraz
$str = 'http://www.google.com';
$str = preg_replace('#^https?://#', '', $str);
echo $str; // www.google.com
That will work for both http://
and https://
这将为双方的工作http://
和https://
回答by Shiplu Mokaddim
You don't need regular expression at all. Use str_replaceinstead.
您根本不需要正则表达式。请改用str_replace。
str_replace('http://', '', $subject);
str_replace('https://', '', $subject);
Combined into a single operation as follows:
合并为单个操作如下:
str_replace(array('http://','https://'), '', $urlString);
回答by Ifti Mahmud
Better use this:
更好地使用这个:
$url = parse_url($url);
$url = $url['host'];
echo $url;
Simpler and works for http://
https://
ftp://
and almost all prefixes.
更简单,适用于http://
https://
ftp://
几乎所有前缀。
回答by manufosela
To remove http://domain( or https ) and to get the path:
删除http://domain(或 https)并获取路径:
$str = preg_replace('#^https?\:\/\/([\w*\.]*)#', '', $str);
echo $str;
回答by Brian Lewis
Yeah, I think that str_replace() and substr() are faster and cleaner than regex. Here is a safe fast function for it. It's easy to see exactly what it does. Note: return substr($url, 7) and substr($url, 8), if you also want to remove the //.
是的,我认为 str_replace() 和 substr() 比正则表达式更快更干净。这是它的安全快速功能。很容易看出它的作用。注意:返回 substr($url, 7) 和 substr($url, 8),如果你还想去掉 //.
// slash-slash protocol remove https:// or http:// and leave // - if it's not a string starting with https:// or http:// return whatever was passed in
function universal_http_https_protocol($url) {
// Breakout - give back bad passed in value
if (empty($url) || !is_string($url)) {
return $url;
}
// starts with http://
if (strlen($url) >= 7 && "http://" === substr($url, 0, 7)) {
// slash-slash protocol - remove https: leaving //
return substr($url, 5);
}
// starts with https://
elseif (strlen($url) >= 8 && "https://" === substr($url, 0, 8)) {
// slash-slash protocol - remove https: leaving //
return substr($url, 6);
}
// no match, return unchanged string
return $url;
}
回答by Erik Saunier
<?php
// (PHP 4, PHP 5, PHP 7)
// preg_replace — Perform a regular expression search and replace
$array = [
'https://lemon-kiwi.co',
'http://lemon-kiwi.co',
'lemon-kiwi.co',
'www.lemon-kiwi.co',
];
foreach( $array as $value ){
$url = preg_replace("(^https?://)", "", $value );
}
This code output :
此代码输出:
lemon-kiwi.co
lemon-kiwi.co
lemon-kiwi.co
www.lemon-kiwi.co
See documentation PHP preg_replace
请参阅文档PHP preg_replace
回答by Overv
If you insist on using RegEx:
如果您坚持使用 RegEx:
preg_match( "/^(https?:\/\/)?(.+)$/", $input, $matches );
$url = $matches[0][2];