0

I am trying to find whether a string contains a certain text or not using strstr()

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

But this gives false. Why is it giving fasle? How to fix it?

4

4 回答 4

2

您的参数是倒置的(请参阅 参考资料strstr)。这是正确的使用方法:

strstr($t, '/image/');
于 2013-08-07T02:25:17.283 回答
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";
}
?>
于 2013-08-07T02:26:34.813 回答
0

试试这样

<?php
$t = "http://site.com/image/d2737cda28cb420c972f7a0ce856cf22";
var_dump(strstr($t, '/image/'));
exit;
?>
于 2013-08-07T02:28:16.487 回答
0

您可以查看该函数的文档。

验证语法。

php/文档/function.strstr.php

正确的用法是: var_dump(strstr($t, '/image/'));

于 2013-08-07T02:35:13.373 回答