php教程

mb_strrpos

(PHP 4 >= 4.0.6, PHP 5, PHP 7)

mb_strrpos — 查找字符串在一个字符串中最后出现的位置

说明

mb_strrpos ( string $haystack , string $needle [, int $offset = 0 [, string $encoding = mb_internal_encoding() ]] ) : int

基于字符数执行一个多字节安全的 strrpos() 操作。 needle 的位置是从 haystack 的开始进行统计的。 第一个字符的位置是 0,第二个字符的位置是 1。

参数

haystack

查找 needle 在这个 string 中最后出现的位置。

needle

在 haystack 中查找这个 string。

offset
可以用于指定 string 里从任意字符数开始进行搜索。 负数的值将导致搜索会终止于指向 string 末尾的任意点。
encoding

encoding 参数为字符编码。如果省略,则使用内部字符编码。

返回值

返回 string 的 haystack 中,needle 最后出现位置的数值。 如果没有找到 needle,它将返回 FALSE。

更新日志

版本 说明
5.2.0 增加了可选参数 offset。

注释

Note: 从 PHP 5.2.0 开始,encoding 参数从第三个位置移到了第四个位置。 为实现向后兼容,可以将第三个参数指定为 encoding,但不建议这么做,在将来会移除这个特性。

参见

  • mb_strpos() - 查找字符串在另一个字符串中首次出现的位置
  • mb_internal_encoding() - 设置/获取内部字符编码
  • strrpos() - 计算指定字符串在目标字符串中最后一次出现的位置

User Contributed Notes

Anonymous 08-Apr-2015 02:20
"Negative values will stop searching at an arbitrary point prior to the end of the string. " ist misleading.
The needle may not fully part of searchrange, defined by a negative offset.
A negative offsets marks the last byte, where a search could start.
<?php
$test
= "Hallo, Herr G?tt";
var_dump(strlen($test));                       // int(17)
var_dump(mb_strrpos($test,'?tt',13));  // int(13)
var_dump(mb_strrpos($test,'?tt',-4)); // int(13) 17-4 = 13
var_dump(mb_strrpos($test,'?tt',-5)); // bool(false)
?>
27-Nov-2005 06:32
mb_strrpos throws a warning if $haystack is empty.
strrpos simply returns FALSE.

This is something to be wary of if overloading the mb functions.

CopyRight © 2008-2022 verySource.Com All Rights reserved. 京ICP备17048824号-1 京公网安备:11010502034788