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递归遍历多维数组实现无限分类的方法

本文实例讲述了PHP递归遍历多维数组实现无限分类的方法。分享给大家供大家参考,具体如下: <?php //$data[]=array('id'=>1,'pa...

一段实用的php验证码函数

本文实例为大家分享了几段php验证码函数,都很实用,具体内容如下 代码段一:简单php验证码函数 <?php function code(){ $im =...

phpmailer简单发送邮件的方法(附phpmailer源码下载)

本文实例讲述了phpmailer简单发送邮件的方法。分享给大家供大家参考,具体如下: 首先,点击此处本站下载相应的php文件。 解压后有2个php文件(2个类)  1个html...

PHP根据图片色界在不同位置加水印的方法

本文实例讲述了PHP根据图片色界在不同位置加水印的方法。分享给大家供大家参考。具体实现方法如下: function add_wm($nmw_water, $src_file, $ou...

PHP文件上传判断file是否己选择上传文件的方法

本文实例讲述了PHP文件上传判断file是否己选择上传文件的方法。分享给大家供大家参考。具体方法如下: 一个合格的程序员在实现数据入库中时我们都会有一些非常严密的过滤与数据规则,像我们文...