php下使用strpos需要注意 === 运算符

yipeiwu_com6年前PHP代码库
复制代码 代码如下:

<?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
?>

相关文章

CentOS安装php v8js教程

CentOS release 5.11 (Final),CentOS release 6.6 (Final) x64测试通过。 gcc版本,glibc版本,libstdc++.so.6版...

PHP 取多维数组指定一列的函数

PHP中对多维数组特定列的提取,是个很常用的功能,有一个专用的函数array_column()用PHP内置的 array_column() 函数是最简单的方法,限制是PHP版本必须是5.5.0及以上版...

PHP正则的Unknown Modifier错误解决方法

如下正则: $a='2<span><nobr>tóng<span class="h">dòng</span></nobr>...

php中异常处理方法小结

本文实例总结了php中异常处理方法。分享给大家供大家参考。具体分析如下: 当异常被触发时,通常会发生:在PHP5中添加了类似于其它语言的错误异常处理模块。在 PHP代码中所产生的异常可被...

php组合排序简单实现方法

php组合排序简单实现方法

本文实例讲述了php组合排序简单实现方法。分享给大家供大家参考,具体如下: 今天被一个组合排序纠结了一晚上,可能是开始没转过弯,所以没想到用二个栈。用了二个栈就很简单的完成了需求效果...