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+xml结合Ajax实现点赞功能完整实例

本文实例讲述了php+xml结合Ajax实现点赞功能的方法。分享给大家供大家参考。具体如下: 使用xml、php和Ajax实现点赞功能,不需要链接数据库,使用php来修改xml的内容,使...

PHP获取用户访问IP地址的5种方法

本文实例为大家分享了5种PHP获取用户访问IP地址的方法,供大家参考,具体内容如下 <?php...

php5新改动之短标记启用方法

第一是按PHP的标准写法.<?php.....?> 第二是在配置文件中(php.ini)修改short_open_tag的值为on....

php下尝试使用GraphicsMagick的缩略图功能

php下尝试使用GraphicsMagick的缩略图功能

常用的图片处理工具有GD,ImageMagick,GraphicsMagick等等。GD就是个阿斗,略过不提;ImageMagick是目前最流行的图片处理工具,它的功能非常丰富;Grap...

PHP SQLite类

复制代码 代码如下:<? /** * SQLite类 * 2009-5-6 * 连万春 * */ class SQLite {     //...