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实现搜索时记住状态的方法。分享给大家供大家参考,具体如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tra...

项目中应用Redis+Php的场景

前言 一些案例中有的同学说为什么不可以用string类型,string类型完全可以实现呀 我建议你看下我的专栏文章《Redis高级用法》,里面介绍了用hash类型的好处 商品维度计数...

示例详解Laravel的注册重构

1. 首先确定用户注册的路由 我们在安装好laravel的时候默认生成的注册是用邮箱进行注册的,并且有些选项不需要,有些还需要加一些表单选项 我们注册的话,并不是可以随便注册的,只有一...

PHP封装的远程抓取网站图片并保存功能类

本文实例讲述了PHP封装的远程抓取网站图片并保存功能类。分享给大家供大家参考,具体如下: <?php /** * 一个用于抓取图片的类 * * @package...

PHP输出数组中重名的元素的几种处理方法

1.可以直接用php的内置函数array_intersect() array array_intersect ( array $array1 , array $array2 [, arr...