strpos查找字符串的注意事项

来源:互联网 发布:excel数据标签连线 编辑:程序博客网 时间:2024/06/05 20:27

不要直接 if(strpos($mystring,$findme)),这样或导致在第一个字符位置被查找到的字符位置返回0,这样就错了。

 

要使用 !==false 这样的判断,注意是两个==

<?php
$mystring 
'abc';
$findme   'a';
$pos strpos($mystring$findme);

// 使用 !== 操作符。使用 != 不能像我们期待的那样工作,
// 因为 'a' 的位置是 0。语句 (0 != false) 的结果是 false。
if ($pos !== false) {
     echo 
"The string '$findme' was found in the string '$mystring'";
         echo 
" and exists at position $pos";
} else {
     echo 
"The string '$findme' was not found in the string '$mystring'";
}
?>

0 0
原创粉丝点击