php mb库,PHP之mb_strrpos使用

mb_strrpos

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

mb_strrpos — Find position of last occurrence of a string in a string

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

Description

int mb_strrpos (

string $haystack ,

string $needle [,

int $offset = 0 [,

string $encoding = mb_internal_encoding() ]]

)

//Performs a multibyte safe strrpos() operation based on the number of characters. needle position is counted from //the beginning of haystack. First character's position is 0. Second character position is 1.

//基於字符數執行一個多字節安全的 strrpos() 操作。 needle 的位置是從 haystack 的開始進行統計的。 第一個字符的位置是 0,第二個字符的位置是 1。

Parameters

haystack

The string being checked, for the last occurrence of needle

查找 needle 在這個 string 中最后出現的位置。

needle

The string to find in haystack.

在 haystack 中查找這個 string。

offset

May be specified to begin searching an arbitrary number of characters into the string. Negative values will stop searching at an arbitrary point prior to the end of the string.

可以用於指定 string 里從任意字符數開始進行搜索。 負數的值將導致搜索會終止於指向 string 末尾的任意點。

encoding

The encoding parameter is the character encoding. If it is omitted, the internal character encoding value will be used.

encoding 參數為字符編碼。如果省略,則使用內部字符編碼。

Return Values

Returns the numeric position of the last occurrence of needle in the haystack string. If needle is not found, it returns FALSE.

返回 string 的 haystack 中,needle 最后出現位置的數值。 如果沒有找到 needle,它將返回 FALSE。

Example

/**

* Created by PhpStorm.

* User: zhangrongxiang

* Date: 2018/2/4

* Time: 下午11:02

*/

$test = "萬物皆對象,everything is object";

echo strlen( $test ) . PHP_EOL; //38 = 6*3 = 20

echo mb_strlen( $test ) . PHP_EOL;//26 = 6 + 20

echo mb_strrpos( $test, '對象', 1 ) . PHP_EOL; // 3

echo mb_strrpos( $test, 'object', 2 ) . PHP_EOL; // 20

echo mb_strrpos( $test, '對象', - 1 ) . PHP_EOL; // 3

echo mb_strrpos( $test, '對象', - mb_strlen( $test ) + 10 ) . PHP_EOL; // 3

echo mb_strrpos( $test, 'object', - 1 ) . PHP_EOL; // 20

echo mb_strrpos( $test, 'object', - 7 ) . PHP_EOL; // false

echo mb_strrpos( $test, '萬物皆對象' ) . PHP_EOL; // 0

//運算符優先級!! 賦值 < 比較

if ( ( $position = mb_strrpos( $test, "萬物皆對象" ) ) !== false ) {

//萬物皆對象

echo mb_substr( $test, $position, 5 ) . PHP_EOL;

}

$str = "https://github.com/zhangrxiang";

if ( ( $position = mb_strrpos( $str, "/" ) ) !== false ) {

//zhangrxiang

echo mb_substr( $str, $position + 1 ) . PHP_EOL;

}

$str = "/Users/zhangrongxiang/WorkSpace/phpProjects/PHPTEST/2018/02/04/mb_strrpos.php";

if ( ( $position = mb_strrpos( $str, "/" ) ) !== false ) {

//mb_strrpos.php

echo mb_substr( $str, $position + 1 ) . PHP_EOL;

}

文章參考

轉載注明出處

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值