2010-12-20 185 views
14

我很奇怪,爲什麼這個代碼不工作:檢查是否字符串包含「HTTP://」

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

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

如果它發現該字符串不包含「HTTP://」,最後一個字符串如果它在前面持有「http://」,則爲「HTTP:// HTTP://foo.foo」。

+0

如果是這樣的實際癥結所在,您可能還需要使用'stripos',所以它找到大寫HTTP://也與' if(stripos($ URL,「http://」)=== 0)' – mario 2010-12-20 08:24:40

+0

您可能會發現['s($ str) - > startsWithIgnoreCase('http://')'](https:// github 。com/delight -im/PHP-Str/blob/8fd0c608d5496d43adaa899642c1cce047e076dc/src/Str.php#L81)有幫助,在[這個獨立的庫](https://github.com/delight-im/PHP-Str)中找到。 – caw 2016-07-27 03:10:09

回答

34

因爲它返回0的字符串,其值爲false。字符串是零索引,因此如果http://是在字符串的開頭找到,該位置爲0,而不是1

你需要它比較嚴格的不平等使用!==到布爾值false:

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

可能'if(substr($ url,0,7)=='http://')'可能會更快。但是,這不是我們應該進行任何優化的地方;-) – zerkms 2010-12-20 08:07:56

+0

@zerkms:哇,你說得對。你又對了。 :P – BoltClock 2010-12-20 08:12:39

+0

@BoltClock:你檢查了嗎?好奇地看到ideone的結果,例如;-) – zerkms 2010-12-20 08:14:59

9

@ 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一起來確定http是否存在於URL字符串中。

$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 
{ 

} 

編輯:

您可以使用$url["scheme"]=="http"作爲@mario建議,與其in_array(),這將是這樣做的更好的辦法:d

+2

嗯,而不是'in_array()'你可以使用'($ url [「scheme」] ==「http」)''。 – mario 2010-12-20 08:13:07

+0

我剛剛提到它用於像'parse_url(「ftp:// http?http#http」)' – mario 2010-12-20 08:16:24

+0

@mario公平點的奇怪邊緣情況。編輯原創答案:) – 2010-12-20 08:36:41

0

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

請注意函數返回的內容。如果字符串匹配,則返回0.對於其他返回值,您可以檢查PHP文檔。還有一個參數來檢查區分大小寫的字符串。如果你指定它爲TRUE,那麼它將檢查大寫字母。

因此,你可以簡單地寫在你的問題如下:

if((substr_compare($URL,"http://",0,7)) === 0) $URL = $URL; 
else $URL = "http://$URL"; 
5
if(preg_match("@^http://@i",$String)) 
    $String = preg_replace("@(http://)[email protected]",'http://',$String); 
else 
    $String = 'http://'.$String; 
1

你檢查,如果字符串包含「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> 

享受傢伙...

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; 
} 
-2

一號線的解決方案:

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

您必須_check_,而不是_create_ URL。 – czachor 2015-02-17 21:45:41

相關問題