为什么这个strstr()返回false?

时间:2013-08-07 02:22:43

标签: php strstr

我正在尝试使用strstr()

查找字符串是否包含某个文本
$t = "http://site.com/image/d2737cda28cb420c972f7a0ce856cf22";
var_dump(strstr('/image/', $t));
exit;

但这会给false。为什么要给予fasle?如何解决?

4 个答案:

答案 0 :(得分:2)

您的参数已反转(请参阅strstr)。这是使用它的正确方法:

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

答案 1 :(得分:2)

你应该使用strpos,更快,更少的资源,使用手册与你的vars

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

答案 2 :(得分:0)

尝试这样做

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

答案 3 :(得分:0)

您可以看到该功能的文档。

验证语法。

php/documentation/function.strstr.php

正确使用:var_dump(strstr($ t,'/ image /'));