[英]Why is this strstr() returning false?
I am trying to find whether a string contains a certain text or not using strstr() 我正在尝试使用strstr()查找字符串是否包含特定文本
$t = "http://site.com/image/d2737cda28cb420c972f7a0ce856cf22";
var_dump(strstr('/image/', $t));
exit;
But this gives false
. 但这给出了
false
。 Why is it giving fasle? 为什么给人烦恼? How to fix it?
如何解决?
you should use strpos , faster, less resources, from the manual with your vars 您应该使用vars手册中的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";
}
?>
try like this instead 像这样尝试
<?php
$t = "http://site.com/image/d2737cda28cb420c972f7a0ce856cf22";
var_dump(strstr($t, '/image/'));
exit;
?>
You can see the documentation of the function. 您可以查看该功能的文档。
Validates the syntax. 验证语法。
php/documentation/function.strstr.php PHP /文档/ function.strstr.php
The correct use would: var_dump(strstr($t, '/image/')); 正确的用法是:var_dump(strstr($ t,'/ image /'));;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.