【问题标题】:Add http:// prefix to URL when missing缺少时将 http:// 前缀添加到 URL
【发布时间】:2011-09-08 14:01:59
【问题描述】:

你好,我有一个非常简单的代码

<a href="'.$aProfileInfo['Website'].'" target="_self">
    <div class="callButton">Website</div>
</a>

问题是如果用户不输入 http:// 链接将指向我的网站,而不是应该指向的外部网站。

如果用户没有输入http://,如何在PHP中签入,没有时自动添加?

【问题讨论】:

标签: php http html anchor href


【解决方案1】:

我认为你最好使用内置函数parse_url(),它返回一个关联数组及其组件

这样的东西对你有用:

 if  ( $ret = parse_url($url) ) {

      if ( !isset($ret["scheme"]) )
       {
       $url = "http://{$url}";
       }
}

【讨论】:

  • 这才是正道!
  • 正则表达式的答案很酷,但我同意这是正确的做法。
  • 如果用户输入类似于以下内容将失败:foo:bar@foobar/p?arg=val#a « foo » 将被解释为方案。
  • 您可以简单地连接// 而不是http 以使用当前页面的方案(http / https)。
  • 我想这会强制使用相同的网站协议,因此如果您的网站位于 https:// 下,所有链接都将变为 https:// 并且它们可能没有 https:// /网址
【解决方案2】:

我个人使用这个,部分取自 php docs

$scheme = parse_url($link, PHP_URL_SCHEME);
if (empty($scheme)) {
    $link = 'http://' . ltrim($link, '/');
}

【讨论】:

  • 这绝对应该是第一个答案。完美!
  • HTTPS 是新标准,应该更新为 https
【解决方案3】:

一个可能不适用于所有情况的简单解决方案(即“https://”):

if (strpos($aProfileInfo['Website'],'http://') === false){
    $aProfileInfo['Website'] = 'http://'.$aProfileInfo['Website'];
}

【讨论】:

  • strpos 不需要先干草堆,然后再用针吗?
【解决方案4】:

解决这个问题有两种方法:url解析和正则表达式。

有些人会说 url 解析是正确,但正则表达式在这种情况下同样有效。我喜欢能够为此类事情使用简单的单行代码,尤其是因为这在模板文件中很常见,您可能需要在 echo 语句中使用单行代码以保持可读性。

正则表达式

我们可以用preg_replace在一个函数调用中做到这一点。

preg_replace('/^(?!https?:\/\/)/', 'http://', $aProfileInfo['Website'])

这在查找http://https:// 的字符串的开头使用negative lookahead。如果找到任何一个,则不会发生替换。如果他们没有找到,它会用http:// 替换字符串的开头(0 个字符),本质上是在不修改的情况下将其添加到字符串中。

在上下文中:

<a href="'. preg_replace('/^(?!https?:\/\/)/', 'http://', $aProfileInfo['Website']).'" target="_self">
    <div class="callButton">Website</div>
</a>

网址解析

(parse_url($aProfileInfo['Website'], PHP_URL_SCHEME) ? '' : 'http://') . $aProfileInfo['Website']

它的作用是通过parse_url($aProfileInfo['Website'], PHP_URL_SCHEME) 找出链接上是否存在方案。然后使用三元运算符,如果找到则输出'',如果找不到则输出'http://'。然后它将链接附加到上面。

在上下文中:

<a href="'.((parse_url($aProfileInfo['Website'], PHP_URL_SCHEME) ? '' : 'http://') . $aProfileInfo['Website']).'" target="_self">
    <div class="callButton">Website</div>
</a>

【讨论】:

  • 您的正则表达式很聪明,它保留了https://,但它不适用于//www.example.com 等相对网址。它还将http:// 附加到ftp:// 或其他协议上。试试这个:preg_replace('/^\/\/|^(?!https?:)(?!ftps?:)/', 'http://', $src)(注意:如果不需要,可以删除对 ftp 的检查)
【解决方案5】:

你可以使用strpos:

// Trim trailing whitespace
$aProfileInfo['Website'] = trim($aProfileInfo['Website']);

// Test if the string begins with "http://"
if (strpos($aProfileInfo['Website'], 'http://') !== 0) {
  $aProfileInfo['Website'] = 'http://' . $aProfileInfo['Website'];
}

【讨论】:

    【解决方案6】:

    你可以使用这个函数作为一个通用的如果在字符串中没有从数组中找到任何东西添加一些东西到它。

    function httpify($link, $append = 'http://', $allowed = array('http://', 'https://')){
    
      $found = false;
      foreach($allowed as $protocol)
        if(strpos($link, $protocol) !== 0)
          $found = true;
    
      if($found)
        return $link;
      return $append.$link;
    }
    

    【讨论】:

      【解决方案7】:

      您还可以考虑到“http(s)”必须在 url 的开头:

      if (preg_match('/^https?:\/\//', $aProfileInfo['Website']) === 0) {
          $aProfileInfo['Website'] = 'http://'.$aProfileInfo['Website'];
      }
      

      【讨论】:

        【解决方案8】:

        这样的?

        if (!strpos($aProfileInfo['Website'], 'http://')) {
            $aProfileInfo['Website'] = 'http://' . $aProfileInfo['Website'];
        }
        

        【讨论】:

          【解决方案9】:

          这是string subtraction的另一个例子:

          $changeLink = $myRow->site_url;
            if(substr($changeLink, 0, 7) != 'http://') {
               $changeLink = 'http://' . $changeLink;  
          }
          

          // ....

          echo "<a href=\"" . $changeLink . "\" target=\"_blank\"></a>";
          

          【讨论】:

            【解决方案10】:

            我相信David's answer 是正确的做法,但可以这样简化:

            parse_url($aProfileInfo['Website'], PHP_URL_SCHEME)==''?'http://'.$aProfileInfo['Website']:$aProfileInfo['Website']
            

            【讨论】:

              猜你喜欢
              • 2013-10-29
              • 2012-04-10
              • 2015-12-07
              • 2015-12-06
              • 1970-01-01
              • 1970-01-01
              • 2014-01-26
              • 1970-01-01
              相关资源
              最近更新 更多