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抽奖小程序的实现代码

这个抽奖小程序,在实际的测试环境中也可以用到,比方说测试数据的查询在in条件下,要查询随机的5个id,然后在用ab去压测 复制代码 代码如下:<?php  /** &nb...

用php简单实现加减乘除计算器

用php实现加减乘除计算器。代码很简单哦! 复制代码 代码如下: <?php header("content-type:text/html;charset=utf-8"); ses...

浅谈php(codeigniter)安全性注意事项

1、httponly session一定要用httponly的否则可能被xxs攻击,利用js获取cookie的session_id。 要用框架的ci_session,更长的位数,http...

php中删除字符串中最先出现某个字符的实现代码

复制代码 代码如下: $a = "字符串";$c= explode("要删除的文字", $a, 2); $b = $c[0].$c[1]; explode (PHP 3, PHP 4,...

PHP基于反射机制实现插件的可插拔设计详解

本文实例讲述了PHP基于反射机制实现插件的可插拔设计。分享给大家供大家参考,具体如下: 说PHP和ASP等同的朋友们可以就此打住了,PHP支持反射,而且还是非常的强大。好了,我们开始今天...