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

相关文章

PHP实现的单向散列加密操作示例

本文实例讲述了PHP实现的单向散列加密操作。分享给大家供大家参考,具体如下: 1.加密文件 <?php //sha1_en.php header("content-ty...

PHP无限分类代码,支持数组格式化、直接输出菜单两种方式

复制代码 代码如下: <?php /** +------------------------------------------------ * 通用的树型类 +---------...

合格的PHP程序员必备技能

作为PHP的爱好者,如果你想加入PHP程序的世界,一定要做好充分的准备。 如果想进入大的企业进行底层开发的话必须对互联网各方面的技术原理了解的很清楚,例如apache实现原理。语言方面既...

PHP mb_convert_encoding 获取字符串编码类型实现代码

后来又在手册上找到了is_utf8函数,这样,再结合iconv函数,我的问题就解决了。下面帖出这个函数: 复制代码 代码如下:function is_utf8($string) { re...

PHP实现的增强性mhash函数

今天使用php的加密函数mhash 的时候,报错: Fatal error : Call to undefined function mhash() mhash是php的内置函数但是使用...