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 Post获取不到非表单数据的问题解决办法

问题描述 在使用vue-axios向后端post数据时,PHP端获取不到post的数据。 问题解决 修改php.ini配置 找到php.ini配置文件,查找enable_post_...

使用php将某个目录下面的所有文件罗列出来的方法详解

直接给源代码了:复制代码 代码如下:$current_dir = 'E:/temp/';$dir = opendir($current_dir);echo "direcotry list...

PHP fopen()和 file_get_contents()应用与差异介绍

复制代码 代码如下: $file=fopen("11.txt","r")or exit("Unable to open file!");//fopen打开文件,如果不存在就显示打不开。...

常见的四种POST 提交数据方式(小总结)

HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS、GET、HEAD、POST、PUT、DELETE、TRACE、CONNECT 这几种。其中,POST 一般用来向服务端...

php处理json格式数据经典案例总结

本文实例总结了php处理json格式数据的方法。分享给大家供大家参考,具体如下: 1.json简介: 何为json? 简 单地说,JSON 可以将 JavaScript 对象中表...