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 广告调用类代码(支持Flash调用)

调用方式如下:其中DebugStr这个函数就是类似一个echo。 复制代码 代码如下: DebugStr('$Adv->getContentById($id); $id为广告编号,...

不要轻信 PHP_SELF的安全问题

复制代码 代码如下:<html> <body> <?php if (isset($_REQUEST['submitted']) && $_REQUEST['...

php常用字符串处理函数实例分析

本文实例讲述了php常用字符串处理函数。分享给大家供大家参考。具体分析如下: 这里只提供几个简单常用的函数: chop执行去除空格处理,get_html_translation_tabl...

关于使用key/value数据库redis和TTSERVER的心得体会

先说redisredis是一个类似memcached的key/value存储系统,它支持存储的value类型相对较多,包括string(字符串)、 list(链表)、set(集合)和zs...

PHP5.5安装PHPRedis扩展及连接测试方法

本文实例讲述了PHP5.5安装PHPRedis扩展及连接测试方法。分享给大家供大家参考,具体如下: phpredis是个人觉得最好的一个php-redis客户端,因为其提供的functi...