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

相关文章

Smarty+QUICKFORM小小演示

由于公司需要quickform结合SMARTY的开发模式,最近几天恶补了下,跟大家分享下心得吧,quickform是一个PEAR类库,可以快速生成表单控件及验证表单的JS代码,大家可能觉...

用php+javascript实现二级级联菜单的制作

 大体思路是这样的:为了不让先前的页面刷新,我用iframe潜入了一个二级子页面,用来读取数据库中的数据,最后把想要的数据传递给父级页面,完成数据的选择和转移。 主要程序代码如...

php读取csv数据保存到数组的方法

本文实例讲述了php读取csv数据保存到数组的方法。分享给大家供大家参考。具体分析如下: csv是常用的excel格式的替代品,很多时候我们导出数据是都会导成csv格式的,这样和exce...

php实现微信公众号无限群发

利用微信客服接口进行各类消息的无限群发 sendAllMsg.php <?php /* Author:yf 使用说明:微信公众号无限...

php fread函数使用方法总结

php fread函数用于读取文件(可安全用于二进制文件),其语法是fread(file,length),参数file必需,指规定要读取打开文件,length 必需,指规定要读取的最大字...