preg 替换 https 将不起作用
preg replace https won't work
我想删除 http://
& https://
我内联添加:^https?://
preg_replace(array('/(?i)\b((^https?:////\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))/', '/(^|[^a-z0-9_])@([a-z0-9_]+)/i', '/(^|[^a-z0-9_])#(\w+)/u')
尝试失败,语法错误?
如果您只想删除 http://
和 https://
(这是您的问题),这将起到作用:
$str = preg_replace('/(https?:\/\/)/', '', 'http://example.com');
如果您只想删除以这些协议之一开头的字符串:
$str = preg_replace('/^(https?:\/\/)/', '', 'http://example.com');
紧跟在初始分隔符之后的^
表示"start of the string"。
function remove_http($url) {
$disallowed = array('http://', 'https://');
foreach($disallowed as $d) {
if(strpos($url, $d) === 0) {
return str_replace($d, '', $url);
}
}
return $url;
}
我想删除 http://
& https://
我内联添加:^https?://
preg_replace(array('/(?i)\b((^https?:////\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))/', '/(^|[^a-z0-9_])@([a-z0-9_]+)/i', '/(^|[^a-z0-9_])#(\w+)/u')
尝试失败,语法错误?
如果您只想删除 http://
和 https://
(这是您的问题),这将起到作用:
$str = preg_replace('/(https?:\/\/)/', '', 'http://example.com');
如果您只想删除以这些协议之一开头的字符串:
$str = preg_replace('/^(https?:\/\/)/', '', 'http://example.com');
紧跟在初始分隔符之后的^
表示"start of the string"。
function remove_http($url) {
$disallowed = array('http://', 'https://');
foreach($disallowed as $d) {
if(strpos($url, $d) === 0) {
return str_replace($d, '', $url);
}
}
return $url;
}