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

相关文章

ThinkPHP发送邮件示例代码

ThinkPHP发送邮件示例代码

先在GitHub找到PHPMailer 并下载 https://github.com/PHPMailer/PHPMailer  //PHPMailer的GitHub地址 将下载的PHPM...

php上传大文件失败的原因及应对策略

php上传大文件失败的原因及应对策略

为什么上传大文件总是失败,但是上传小文件就没有问题。小编也不得其解,网上搜其原因,整理了一篇关于php上传大文件失败的原因和解决办法的文章,分享给大家。 下面分别是各种原因以及解决办法...

PHP正则删除html代码中a标签并保留标签内容的方法 原创

本文实例讲述了PHP正则删除html代码中a标签并保留标签内容的方法。分享给大家供大家参考,具体如下: 一、问题: 有HTML代码如: <div>欢迎访问【宜配屋www....

php实现替换手机号中间数字为*号及隐藏IP最后几位的方法

本文实例讲述了php实现替换手机号中间数字为*号及隐藏IP最后几位的方法。分享给大家供大家参考,具体如下: $string = "13826589549"; $pattern = "...

详解PHP多个进程配合redis的有序集合实现大文件去重

详解PHP多个进程配合redis的有序集合实现大文件去重

1.对一个大文件比如我的文件为 -rw-r--r-- 1 ubuntu ubuntu 9.1G Mar 1 17:53 2018-12-awk-uniq.txt 2.使用split命令切...