PHP | strrchr函数

这个 strrchr() 函数是PHP中的内置函数。此函数接受两个参数:字符串和字符。此函数用于搜索给定字符串中的给定字符,并返回从该字符串中最后一次出现的给定字符开始的字符串部分。

null

语法 :

strrchr($string, $key)

参数 :此函数接受两个参数。这两个参数都是强制性的,如下所述:

  • $string :这是我们要在其中搜索给定键的输入字符串。
  • $key :此参数表示要在给定字符串$string中搜索的字符。如果此参数包含多个字符,则只会在$string中搜索此参数的第一个字符。

返回值 :此函数返回$string中从给定$key在该字符串中最后一次出现开始的部分。

例如:

Input : $string = "Hello|welcome|to|gfg"  $key = '|'
Output : |gfg

Input :  $string = "Welcometogfg"  $key = ''
Output : gfg

下面的程序演示了PHP中的strrchr()函数:

方案1 :

<?php
// Input string
$string = "Hello|welcome|to|gfg" ;
// key to be searched
$key = "|" ;
echo strrchr ( $string , $key );
?>


输出:

|gfg

方案2 :当$key包含转义序列时。

<?php
// Input string
$string = "Hellowelcometogfg" ;
// key to be searched
$key = "" ;
echo strrchr ( $string , $key );
?>


输出:


gfg

方案3 :当$key包含多个字符时。

<?php
// Input string
$string = "Hello|welcome|to|gfg" ;
// key to be searched
$key = "|welcome" ;
echo strrchr ( $string , $key );
?>


输出:

|gfg

参考 : http://php.net/manual/en/function.strrchr.php

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享