我试图找出字符串是否包含在字符串中,但它总是正确的。为什么这总是正确的
<?php
$test = 'ORDER BY `views`';
if(strpos($test,'views') !== true) echo 'true';
else echo 'false';
?>
您不正确地使用了strpos()
参数。
您正在使用strpos($needle, $haystack)
但实际上应该strpos($haystack, $needle)
请注意,如果在大海捞针中找不到针,strpos()
返回FALSE
。因此,您需要检查它是否返回FALSE
(而不是TRUE
)。
使用您的代码,它将变为:
if(strpos($test, 'views') !== FALSE)
echo 'true';
else
echo 'false';
演示!
检查这个
$test = 'ORDER BY `views`';
if (strpos($test,'views') !== false) {
echo 'true';
} else {
echo 'false';
}
也使用
您可以使用这些字符串函数,
strstr — 查找字符串的第一个匹配项
stristr — 不区分大小写的 strstr()
strrchr — 查找字符串中最后一个出现的字符
strpos — 查找字符串中子字符串第一次出现的位置
strpbrk — 在字符串中搜索一组字符中的任何一个
如果这没有帮助,那么您应该使用正则表达式preg
preg_match — 执行正则表达式匹配