php下使用strpos需要注意 === 运算符
代码如下:
<?php
/*
判断字符串是否存在的函数
*/
function strexists($haystack, $needle) {
return !(strpos($haystack, $needle) === FALSE);//注意这里的"==="
}
/*
Test
*/
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);
// Note our use of ===. Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
// 简单的使用 "==" 号是不会起作用的,需要使用 "===",因为 a 第一次出现的位置为 0
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";
}
// We can search for the character, ignoring anything before the offset
// 在搜索字符的时候可以使用参数 offset 来指定偏移量
$newstring = 'abcdef abcdef';
$pos = strpos($newstring, 'a', 1); // $pos = 7, not 0
?> 相关推荐
resilient 2019-11-04
88413863 2014-07-13
xianzhe 2019-06-30
会写code的凳子哥 2016-08-01
zuoyanyan 2019-06-28
xishizhaohua 2019-06-26
resilient 2019-06-21
xiaocao 2014-02-25
puddingpp 2019-06-13
phpparty 2018-01-04
IT日志 2019-04-29
jinyusheng 2017-11-16
githubphpnobug 2019-04-17
phptyong 2017-03-15
phpyounger 2014-04-08
wangtengphp 2019-02-27
githubphpnobug 2019-04-02
PHP100 2019-03-28
PHP100 2019-03-28