检查字符串是否包含"HTTP://" [英] Checking if string contains "HTTP://"

查看:150
本文介绍了检查字符串是否包含"HTTP://"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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";

如果确实发现该字符串不包含"HTTP://",则如果最后一个字符串包含"http://",则最终字符串为"HTTP://HTTP://foo.foo".

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.

推荐答案

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

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.

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

You need to compare it for strict inequality to boolean false using !==:

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

这篇关于检查字符串是否包含"HTTP://"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆