PHP strrchr()实例讲解

时间:2022-04-07
本文章向大家介绍PHP strrchr()实例讲解,主要分析其语法、参数、返回值和注意事项,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

PHP 内置函数中的 strrchr() 函数。它用于查找字符串最后一次出现的位置。

此函数返回从该位置到字符串末尾的所有字符,如果未找到该字符,则返回 FALSE。

注意:strrchr() 函数是二进制安全的。

用法:

strrchr(string,char);
参数 描述 必需/可选
String 指定要搜索的字符串。 required
Char 使用 ASCII 值指定要查找的字符串。 required

例子1

<?php
  $str1="Hello PHP";
  $str2="PHP";
  echo "Your first string is:".$str1;
  echo "<br>";
  echo "Your second string is:".$str2;
  echo "<br>";
  echo "By using 'strrchr()' function:".strrchr($str1,$str2);
?>

输出:

Your first string is:Hello PHP
Your second string is:PHP
By using 'strrchr()' function:P

例子2

<?php
  $str="We are Learning PHP";
  echo "Your string is:".$str;
  echo "<br>";
echo "By using 'strrchr()' function:".strrchr("$str",are);
?>

输出:

Your string is:We are Learning PHP
By using 'strrchr()' function:arning PH

例子3

<?php
  $str="Hello PHP";
  echo "Your string is:".$str;
  echo "<br>";
  echo "By using 'strrchr()' function:".strrchr("$str",101);
  // Search a string for the ASCII value of "e" (101) and 
  //return all characters from this position to the end of the string:
?>

输出:

Your string is:Hello PHP
By using 'strrchr()' function:ello PHP

另见:

strncmp():前n个字符的字符串比较(区分大小写)

strpbrk():在字符串中搜索任何一组字符

strpos():返回一个字符串在另一个字符串中第一次出现的位置(区分大小写)

参考资料:

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