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中一些可以替代正则表达式函数的字符串操作函数

0x01:根据预定义的字符对字符串进行词法分析 复制代码 代码如下: <?php /*  * 在处理大量信息时,正则表达式函数会使速度大幅减慢。应当在需要使用正则...

深入解析WordPress中加载模板的get_template_part函数

最近研究官方主题 Twenty Eleven ,有一些东西网上现成的中文资料不好找,在博客里记载下来,算是分享,也算是备忘,wordpress 3.0 以后就开始便有了get_templ...

PHP中的闭包(匿名函数)浅析

闭包也叫匿名函数 PHP5.3 引入。 使用方法 需要调整数组元素中的值 复制代码 代码如下: $data = range(0, 100);//想要每个元素的值都加上.html的后缀 $...

PHP实现的DES加密解密实例代码

本文实例讲述了PHP实现的DES加密解密实例。分享给大家供大家参考,具体如下: <?php $key = 'very important data'; function...

PHP入门教程之面向对象基本概念实例分析

本文实例讲述了PHP面向对象基本概念。分享给大家供大家参考,具体如下: Demo1.php <?php //怎样去创建一个类 格式:修饰符 class 类名{}...