【问题标题】:Checking if string contains "HTTP://"检查字符串是否包含“HTTP://”
【发布时间】:2010-12-20 07:55:18
【问题描述】:

我想知道为什么这段代码不起作用:

// check to see if string contains "HTTP://" in front

if(strpos($URL, "http://")) $URL = $URL;
else $URL = "http://$URL";

如果它确实发现该字符串不包含“HTTP://”,那么最后一个字符串是“HTTP://HTTP://foo.foo”,如果它前面包含“http://”。

【问题讨论】:

标签: php string


【解决方案1】:

因为它为该字符串返回 0,它的计算结果为 false。字符串是零索引的,因此如果在字符串的开头找到 http://,则位置是 0,而不是 1。

您需要使用 !== 将其与布尔值 false 进行严格的不等式比较:

if(strpos($URL, "http://") !== false)

【讨论】:

  • if (substr($url, 0, 7) == 'http://') 可能会更快。但这不是我们应该应用任何优化的地方 ;-)
  • @BoltClock:你检查了吗?好奇地看到 ideone 上的结果,例如 ;-)
  • @zerkms:实际上差不多。我的测试有一个错字导致它检查未定义的变量:|
  • @BoltClock: :-( 似乎在大量数据上差异会很明显。如果 url 是 2kb 长度怎么办?;-)
  • @zerkms:如果strpos() 返回 false,它会慢得多(在 2 KB 字符串上慢 7 倍)。如果strpos() 返回一个整数,则在同一字符串上的速度大致相同。
【解决方案2】:

@BoltClock 的方法会起作用。

或者,如果您的字符串是 URL,您可以使用 parse_url(),它将返回关联数组中的 URL 组件,如下所示:

print_r(parse_url("http://www.google.com.au/"));


Array
(
    [scheme] => http
    [host] => www.google.com.au
    [path] => /
)

scheme 是您所追求的。您可以将parse_url()in_array 结合使用来确定URL 字符串中是否存在http

$strUrl       = "http://www.google.com?query_string=10#fragment";
$arrParsedUrl = parse_url($strUrl);
if (!empty($arrParsedUrl['scheme']))
{
    // Contains http:// schema
    if ($arrParsedUrl['scheme'] === "http")
    {

    }
    // Contains https:// schema
    else if ($arrParsedUrl['scheme'] === "https")
    {

    }
}
// Don't contains http:// or https://
else
{

}

编辑:

您可以按照@mario 的建议使用$url["scheme"]=="http" 而不是in_array(),这将是一种更好的方法:D

【讨论】:

  • 嗯,你可以用($url["scheme"]=="http")代替in_array()
  • 我刚刚提到了像parse_url("ftp://http?http#http")这样的怪异边缘情况@
【解决方案3】:
if(preg_match("@^http://@i",$String))
    $String = preg_replace("@(http://)+@i",'http://',$String);
else
    $String = 'http://'.$String;

【讨论】:

    【解决方案4】:

    您需要记住https://。 试试这个:

    private function http_check($url) {
        $return = $url;
        if ((!(substr($url, 0, 7) == 'http://')) && (!(substr($url, 0, 8) == 'https://'))) {
            $return = 'http://' . $url;
        }
        return $return;
    } 
    

    【讨论】:

      【解决方案5】:

      您已检查字符串是否包含“HTTP://”或不包含

      以下代码完美运行。

      <?php 
      $URL = 'http://google.com';
      $weblink =   $URL; 
          if(strpos($weblink, "http://") !== false){ }
          else { $weblink = "http://".$weblink; }
       ?>
        <a class="weblink" <?php if($weblink != 'http://'){ ?> href="<?php echo $weblink; ?>"<?php } ?> target="_blank">Buy Now</a>
      

      祝大家好运……

      【讨论】:

        【解决方案6】:

        你可以使用 substr_compare() [PHP Docs]

        注意函数返回的内容。如果字符串匹配,则返回 0。 对于其他返回值,您可以查看 PHP 文档。还有一个参数可以检查区分大小写的字符串。如果你指定它为 TRUE,那么它将检查大写字母。

        所以你可以在你的问题中简单地写如下:

        if((substr_compare($URL,"http://",0,7)) === 0) $URL = $URL;
        else $URL = "http://$URL";
        

        【讨论】:

          【解决方案7】:

          一线解决方案:

          $sURL = 'http://'.str_ireplace('http://','',$sURL);
          

          【讨论】:

          • 您必须检查,而不是创建 URL。
          • 其实,这正是OP的代码所做的。
          猜你喜欢
          • 1970-01-01
          • 2011-11-09
          • 2013-05-18
          • 2012-06-30
          • 1970-01-01
          • 2013-10-26
          • 1970-01-01
          • 2012-01-05
          • 2013-11-20
          相关资源
          最近更新 更多