$string = "Test String for the test";
$match = "test string";
如何确定$ match是否在$ string中?
$string = "Test String for the test";
$match = "test string";
如何确定$ match是否在$ string中?
可以在$string
使用stripos
找到$match
的位置不区分大小写的搜索:
$pos = stripos($string, $match);
注有type-safe comparison operator像===
或!==
来进行比较。因为如果$match
在$string
的开头,就像在这种情况下那样,stripos
返回0
和(boolean) 0 === false
。
使用stristr($haystack, $needle)
:http://php.net/manual/en/function.stristr.php
$boolean = stristr($string, $match);
所以,从技术上讲,这会返回''测试测试字符串'',但是这会评估为'TRUE'。 – 2010-11-06 21:44:13
+1为安全比较运算符 – 2010-11-06 21:59:44
比较是什么?如果$ pos> = 0 – 2010-11-06 22:05:10
@Scott B:不,'false> = 0'为真。使用'$ pos!== false'或'$ pos === false''。 – Gumbo 2010-11-06 22:06:29