从字符串中删除http://的PHP正则表达式

从字符串中删除http://的PHP正则表达式,php,regex,Php,Regex,我有完整的URL作为字符串,但我想删除字符串开头的http://以很好地显示URL(例如:www.google.com而不是) 有人能帮忙吗?如果你坚持使用正则表达式: preg_match( "/^(https?:\/\/)?(.+)$/", $input, $matches ); $url = $matches[0][2]; 这将适用于http://和https://为什么不使用正则表达式呢?您根本不需要正则表达式。改用 组合成一个操作,如下所示: str_replace(array('h

我有完整的URL作为字符串,但我想删除字符串开头的http://以很好地显示URL(例如:www.google.com而不是)


有人能帮忙吗?

如果你坚持使用正则表达式:

preg_match( "/^(https?:\/\/)?(.+)$/", $input, $matches );
$url = $matches[0][2];

这将适用于http://和https://为什么不使用正则表达式呢?

您根本不需要正则表达式。改用

组合成一个操作,如下所示:

str_replace(array('http://','https://'), '', $urlString);
最好使用这个:

$url = parse_url($url);  
$url = $url['host'];

echo $url;
更简单,适用于
http://
https://
ftp://
和几乎所有前缀。

要删除(或https)并获取路径:

   $str = preg_replace('#^https?\:\/\/([\w*\.]*)#', '', $str);
   echo $str;

是的,我认为str_replace()和substr()比regex更快更干净。这里有一个安全快速的功能。很容易看出它的确切功能。注意:如果还要删除//,请返回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;
}

请参阅文档

为什么需要正则表达式?为什么不删除前7个字符呢?检查这一个:@OliCharlesworth:它可以是8个字符,如果不需要使用正则表达式,请不要使用<代码>str_replace比regex更快,并且更容易让其他人阅读您的代码。可能是的重复。为了完整起见,我会在http之后添加一个
s?
。是的,我知道这不是他的问题…:)来自一个对regex不太了解的人,这是解决这个问题最容易理解和实现的解决方案之一,非常感谢。如果http是大写的,regex会有什么变化@sarfrazThis还将删除任何后续匹配的http(s):/,这可能不是问题,但可能是。例如,如果在没有正确URL编码的查询字符串中使用它,很高兴你们喜欢它:),但它会丢弃任何路径和查询信息以及传输协议。因此,虽然它在OPs示例中成功地工作,但实际上它并不是他们问题的正确答案。@piersb我不能完全同意你的观点。代码成功地传递了Casey想要的结果。此外,它是为了一个目的而写的。如果您想显示路径或查询信息,当然可以这样做()。但是,我发现代码有一个问题。如果我们试图在不指定协议的情况下解析url,它会显示一个错误,这正是我现在感到恼火的地方:/
   $str = preg_replace('#^https?\:\/\/([\w*\.]*)#', '', $str);
   echo $str;
// 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;
}
<?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 );
}
lemon-kiwi.co
lemon-kiwi.co
lemon-kiwi.co
www.lemon-kiwi.co