2010-11-06 55 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

相關問題