2013-08-07 97 views
0

我試圖找到一個字符串是否包含了一定的文本或不使用的strstr()爲什麼這個strstr()返回false?

$t = "http://site.com/image/d2737cda28cb420c972f7a0ce856cf22"; 
var_dump(strstr('/image/', $t)); 
exit; 

但是這給false。爲什麼它給予fasle?如何解決它?

+1

你應該使用strpos,更快,更少的資源 – 2013-08-07 02:25:18

+0

明確的證據表明Jedi寫了php。 「在乾草堆裏,你必須找到一根針。」 – Floris

回答

2

您的參數反轉(請參閱strstr)。這是使用它的正確方法:

strstr($t, '/image/'); 
+0

噢...爲什麼PHP總是與'needle'和'haystack'混淆? – mrN

+0

@mrN這是一個很好的問題。很隨意。 – federicot

+0

probaby,因爲它基於C版本? http://linux.die.net/man/3/strstr – hdgarrood

2

應與您的增值經銷商使用strpos,更快,更少的資源,從手動

<?php 
$t = "http://site.com/image/d2737cda28cb420c972f7a0ce856cf22"; 
$findme = '/image/'; 
$pos = strpos($t, $findme); 

// Note our use of ===. Simply == would not work as expected 
// because the position of 'a' was the 0th (first) character. 
if ($pos === false) { 
    echo "The string '$findme' was not found in the string '$mystring'"; 
} else { 
    echo "The string '$findme' was found in the string '$mystring'"; 
    echo " and exists at position $pos"; 
} 
?> 
+0

如果只是'if(!$ pos)'?那會省略'==='的需要嗎? – mrN

+0

閱讀手冊頁上的警告 – 2013-08-07 02:36:59

+0

我接受了其他答案,因爲這是我的問題的答案。但我正在使用你的解決方案。 +1 – mrN

0

嘗試這樣反而

<?php 
$t = "http://site.com/image/d2737cda28cb420c972f7a0ce856cf22"; 
var_dump(strstr($t, '/image/')); 
exit; 
?>