php 检查字符串是否包含“HTTP://”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4487794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking if string contains "HTTP://"
提问by somewalri
I am wondering why this code is not working:
我想知道为什么这段代码不起作用:
// check to see if string contains "HTTP://" in front
if(strpos($URL, "http://")) $URL = $URL;
else $URL = "http://$URL";
If it does find that the string doesn't contain "HTTP://" the final string is "HTTP://HTTP://foo.foo" if it contiains "http://" in front.
如果它确实发现该字符串不包含“HTTP://”,则最终字符串是“HTTP://HTTP://foo.foo”(如果它在前面包含“http://”)。
回答by BoltClock
Because it's returning 0 for that string, which evaluates to false. Strings are zero-indexed and as such if http://
is found at the beginning of the string, the position is 0, not 1.
因为它为该字符串返回 0,结果为 false。字符串是零索引的,因此如果http://
在字符串的开头找到,则位置为 0,而不是 1。
You need to compare it for strict inequality to boolean false using !==
:
您需要使用以下方法将其严格不等式与布尔假进行比较!==
:
if(strpos($URL, "http://") !== false)
回答by Russell Dias
@BoltClock's method will work.
@BoltClock 的方法会起作用。
Alternatively, if your string is a URL you can use parse_url(), which will return the URL components in an associative array, like so:
或者,如果您的字符串是一个 URL,您可以使用parse_url(),它将返回关联数组中的 URL 组件,如下所示:
print_r(parse_url("http://www.google.com.au/"));
Array
(
[scheme] => http
[host] => www.google.com.au
[path] => /
)
The scheme
is what you're after. You can use parse_url()in conjunction with in_array
to determine if http
exists within the URL string.
这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
{
}
Edit:
编辑:
You can use $url["scheme"]=="http"
as @mario suggested instead of in_array()
, this would be a better way of doing it :D
您可以$url["scheme"]=="http"
按照@mario 的建议使用,而不是使用in_array()
,这将是更好的方法:D
回答by Serhat
if(preg_match("@^http://@i",$String))
$String = preg_replace("@(http://)+@i",'http://',$String);
else
$String = 'http://'.$String;
回答by Daniel
You need to remember about https://
.
Try this:
你需要记住关于https://
. 尝试这个:
private function http_check($url) {
$return = $url;
if ((!(substr($url, 0, 7) == 'http://')) && (!(substr($url, 0, 8) == 'https://'))) {
$return = 'http://' . $url;
}
return $return;
}
回答by Bhavesh Godhani
you have checking if string contains “HTTP://” OR Not
您已检查字符串是否包含“HTTP://”或不
Below code is perfectly working.
下面的代码是完美的工作。
<?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>
Enjoy guys...
享受伙计们...
回答by SilentAssassin
You can use substr_compare()[PHP Docs].
您可以使用 substr_compare() [PHP Docs]。
Be careful about what the function returns. If the strings match, it returns 0.For other return values you can check the PHP docs. There is also a parameter to check case-sensitive strings. If you specify it TRUE then it will check for upper-case letters.
注意函数返回的内容。如果字符串匹配,则返回 0。对于其他返回值,您可以查看 PHP 文档。还有一个参数来检查区分大小写的字符串。如果您将其指定为 TRUE,则它将检查大写字母。
So you can simply write as follows in your problem:
所以你可以简单地在你的问题中写如下:
if((substr_compare($URL,"http://",0,7)) === 0) $URL = $URL;
else $URL = "http://$URL";
回答by Ronald
One line solution:
一行解决方案:
$sURL = 'http://'.str_ireplace('http://','',$sURL);