php下使用strpos需要注意 === 运算符

yipeiwu_com5年前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编程过程中需要了解的this,self,parent的区别

{一}PHP中this,self,parent的区别之一this篇 面向对象编程(OOP,Object Oriented Programming)现已经成为编程人员的一项基本技能。利用O...

PHPAnalysis中文分词类详解

PHPAnalysis是目前广泛使用的中文分词类,使用反向匹配模式分词,因此兼容编码更广泛,现将其变量与常用函数详解如下: 一、比较重要的成员变量 $resultType &n...

PHP改进计算字符串相似度的函数similar_text()、levenshtein()

similar_text()中文汉字版 复制代码 代码如下:      <?php     ...

header导出Excel应用示例

复制代码 代码如下: <?php class reportFormAction extends CommonAction{ public function index(){ if(...

PHP过滤黑名单关键字的方法

本文实例讲述了PHP过滤黑名单关键字的方法。分享给大家供大家参考。具体实现方法如下: 关键字过滤非常的简单把要过滤的内容存在数组或文档中,然后用户提交时我们进行preg_match或is...