2010-11-06 50 views
1
$string = "Test String for the test"; 
$match = "test string"; 

如何确定$ match是否在$ string中?

回答

2

可以在$string使用stripos找到$match的位置不区分大小写的搜索:

$pos = stripos($string, $match); 

注有type-safe comparison operator===!==来进行比较。因为如果$match$string的开头,就像在这种情况下那样,stripos返回0(boolean) 0 === false

+0

+1为安全比较运算符 – 2010-11-06 21:59:44

+0

比较是什么?如果$ pos> = 0 – 2010-11-06 22:05:10

+0

@Scott B:不,'false> = 0'为真。使用'$ pos!== false'或'$ pos === false''。 – Gumbo 2010-11-06 22:06:29

0

使用stristr($haystack, $needle)http://php.net/manual/en/function.stristr.php

$boolean = stristr($string, $match); 
+0

所以,从技术上讲,这会返回''测试测试字符串'',但是这会评估为'TRUE'。 – 2010-11-06 21:44:13

1

使用strpos检查,如果它是的字符串中。 API Link

对于不区分大小写,请使用striposAPI Link

相关问题