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 //创建压缩图 function _create_thumbnail($srcF...

Smarty foreach控制循环次数的实现详解

1.可以用{$array| count} 来试试。2.复制代码 代码如下:{foreach from=$variable key=key name=name iteam=value}&n...

php生成二维码的几种方式整理及使用实例

php生成二维码的几种方式整理及使用实例

1.google开放api 复制代码 代码如下: $urlToEncode="http://bbs.lewanchina.com"; generateQRfromGoogle($u...

PHP数据库操作三:redis用法分析

本文实例讲述了PHP数据库操作redis用法。分享给大家供大家参考,具体如下: memcache虽然好用,解决了数据库遇到高并发时的IO问题,但还有很多问题丞待解决: 1、数据持久性问题...

PHP 图像尺寸调整代码

复制代码 代码如下: /********************** *@filename - path to the image *@tmpname - temporary path...