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中spl_autoload_register()和__autoload()区别分析

PHP中spl_autoload_register()和__autoload()区别分析

关于spl_autoload_register()和__autoload(),相信大多数都会选择前者了? 看两者的用法: 复制代码 代码如下://__autoload用法function...

计算一段日期内的周末天数的php代码(星期六,星期日总和)

复制代码 代码如下: /* | Author: Yang Yu <niceses@gmail.com> | @param char|int $start_date 一个有效的...

PHP 5.3 下载时 VC9、VC6、Thread Safe、Non Thread Safe的区别分析

一共给了四个版本,VC9 x86 Non Thread Safe、VC9 x86 Thread Safe、VC6 x86 Non Thread Safe、VC6 x86 Thread S...

php实现多张图片上传加水印技巧

复制代码 代码如下: <?php function imageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterTex...

php实现中文字符截取防乱码方法汇总

大家在自己的程序中相信都会经常用到截取字符串吧,但是往往遇到截取中文字符串的时候会遇到乱码的问题。很是让人头疼,接下来介绍两种方法防止截取中文字符串的时候出现乱码的问题。 首先第一种,自...