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 根据url自动生成缩略图并处理高并发问题

服务器生成缩略图的时机一般分为两种: 1.上传文件时生成 优点:上传时就已经生成需要的缩略图,读取时不需要再判断,减少cpu运算。 缺点:当缩略图尺寸变化时或新增尺寸时,需要重新生成所有...

php 面向对象的一个例子

复制代码 代码如下: <?php class person{ //下面是人的成员属性 var $name; //人的名字 var $sex; //人的性别 var $age; //...

Php注入点构造代码

把下面保存成 Test.asp 复制代码 代码如下:<?   $mysql_server_name = "localhost";&...

分享8个最佳的代码片段在线测试网站

分享8个最佳的代码片段在线测试网站

有时候,我们需要测试一些代码片段,而电脑中没有安装针对该语言的运行环境,没关系,你可以在线测试它们。  本文为你带来 8 款非常好用的代码片段在线工具,帮助你快速、方便地测试、...

PHP实现将标点符号正则替换为空格的方法

本文实例讲述了PHP实现将标点符号正则替换为空格的方法。分享给大家供大家参考,具体如下: <?php $character = "!@#$%^&*中'文中'文中'文()...