1. PHP 函数 strpos ()
看了平台上的@finecho每天坚持学习函数,感觉自己干了那么些年php,好多函数不查字典都不是太清楚,决定向他学习,每天学习几个php。
strpos
(PHP4, PHP5, PHP7)
strpos — 查找字符串首次出现的位置
说明
strpos ( string $haystack , mixed $needle [, int $offset = 0 ] ) : int
返回 needle
在 haystack
中首次出现的数字位置。
参数
haystack :在该字符串中查找
needle :如果needle
不是一个字符串,那么它将被转换为整形并被视为字符的顺序值
offset :如果提供了此参数,搜索会从字符串该字符数的起始位置开始统计。如果是负数,搜索会从字符串结尾指定字符数开始。
返回值
返回needle
存在于haystack
字符串起始的位置(独立于offset
)。同时注意字符串位置是从0开始,而不是从1开始的。
如果没找到needle
,将返回 FALSE。
Warning:此函数可能返回布尔值 FALSE 但也可能返回等同于 FALSE 的非布尔值。应使用
===
运算符来测试此函数的返回值。
更新日志
版本 | 说明 |
---|---|
7.1.0 | 开始支持负数的offset |
范例
Example #1 使用 ===
<?php
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);
// 注意这里使用的是 === 。简单的 == 不能像我们期待的那样工作,
// 因为 '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'";
}
?>
Example #2 使用 !==
<?php
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);
// 使用 !== 操作符。使用 != 不能像我们期待的那样工作,
// 因为 'a' 的位置是 0。语句 ( 0 != false ) 的结果是 false
if ($post !== false) {
echo "The string '$findme' was founf in the string '$mystring'";
echo " and exists at position '$pos'";
} else {
echo "The string '$findme' was not found in the string '$mystring'";
}
?>
Example #3 使用位置偏移量offset
<?php
// 忽视位置偏移量之前的字符进行查找
$newstring = 'abcdef abcdef';
$pos = strpos($newstring, 'a', 1); // $pos = 7, 不是 0
?>
注释
Note:此函数可安全用于二进制对象
参见
- stripos() - 查找字符串首次出现是位置(不区分大小写)
- strrpos() - 计算指定字符串在目标字符串中最后一次出现的位置
- strripos() - 计算指定字符串在目标字符串中最后一次出现的位置(不区分大小写)
- strstr() - 查找字符串的首次出现
- strpbrk() - 在字符串中查找一组字符的任何一个字符
- substr() - 返回字符串的子串
- preg_match() - 执行匹配正则表达式
本作品采用《CC 协议》,转载必须注明作者和本文链接