strrchr Function
strrchr()函数是PHP中的一个内置函数。这个函数有两个参数,一个字符串和一个字符。这个函数搜索给定字符串中的给定字符,并返回从该字符串中给定字符最后一次出现开始的字符串部分。
语法
1 | strrchr($string, $key) |
参数:此函数接受两个参数。这两个参数都是必须的,如下所述:
- $string:这是我们要在其中搜索给定键的输入字符串。
- $key:该参数表示在给定字符串$string中搜索的字符。如果该参数包含多个字符,那么在$string中只搜索该参数的第一个字符。
返回值:这个函数返回从字符串中最后一次出现给定的$key开始的$string部分。
例子
1 2 3 4 5 | Input : $string = "Hello|welcome|to|gfg" $key = '|' Output : |gfg Input : $string = "Welcome\nto\ngfg" $key = '\n' Output : gfg |
程序1
1 2 3 4 5 6 7 8 9 10 11 | <?php // Input string $string = "Hello|welcome|to|gfg"; // key to be searched $key = "|"; echo strrchr($string, $key); ?> |
输出
1 | |gfg |
程序2:当$key包含转义序列时。
1 2 3 4 5 6 7 8 9 10 11 | <?php // Input string $string = "Hello\nwelcome\nto\ngfg"; // key to be searched $key = "\n"; echo strrchr($string, $key); ?> |
输出
1 | gfg |
程序3:当$key包含多个字符时。
1 2 3 4 5 6 7 8 9 10 11 | <?php // Input string $string = "Hello|welcome|to|gfg"; // key to be searched $key = "|welcome"; echo strrchr($string, $key); ?> |
输出
1 | |gfg |
参考