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 解析xml 的四种方法详细介绍

php 解析xml 的四种方法 XML处理是开发过程中经常遇到的,PHP对其也有很丰富的支持,本文只是对其中某几种解析技术做简要说明,包括:Xml parser, SimpleXML,...

smarty模板引擎从配置文件中获取数据的方法

本文实例讲述了smarty模板引擎从配置文件中获取数据的方法。分享给大家供大家参考。具体如下: 当某个变量值,不希望在程序中写死时,就可以把该变量写到配置文件里,并从中获取(常见的配置样...

php安装dblib扩展,连接mssql的具体步骤

1、先安装freetds 然后修改配置文件 不要装0.82版本,会报编译失败 Compile Failure With freetds0.82 进到freetds目录下编译安装 ./co...

更改localhost为其他名字的方法

ctrl + r => 输入drivers回车 => etc/hosts , 用记事本打开它,在 127.0.0.1 localhost 下面增加一行, 127.0.0.1...

php 数组使用详解 推荐

php 数组使用详解 推荐

PHP的数组函数众多,下面是我学习的小结,借此记之,便于以后鉴之……   一、数组定义:   数组的定义使用 array()方式定义,可以定义空数组: .foreach遍历: 复制代码...