PHP function strrchr() to find the last occurrence of a string in another string

黄舟
Release: 2023-03-17 06:48:01
Original
2890 people have browsed it

Example

Search for the position of "world" in string and return all characters from that position to the end of the string:

<?php
echo strrchr("Hello world!","world");
?>
Copy after login

Definition and Usage

strrchr() function finds the last occurrence of a string in another string and returns all characters from that position to the end of the string.

Note: This function is binary safe.

Syntax

strrchr(string,char)
Copy after login
ParametersDescription
string Required. Specifies the string to be searched for.
charRequired. Specifies the characters to search for. If the argument is a number, then searches for characters matching the numeric ASCII value.

Technical details

In PHP 4.3, this function became binary safe.

更多实例

实例 1

搜索 "" 在字符串中的位置,并返回从该位置到字符串结尾的所有字符:

<?php
echo strrchr("Hello world! What a beautiful day!",What);
?>
Copy after login

实例 2

通过 "o" 的 ASCII 值搜索 "o" 在字符串中的位置,并返回从该位置到字符串结尾的所有字符:

<?php
echo strrchr("Hello world!",111);
?>
Copy after login

string strrchr ( string $haystack , mixed $needle )

该函数返回 haystack 字符串中的一部分,这部分以 needle 的最后出现位置开始,直到 haystack 末尾。

<?php
$str = "这四个函数是字符操作函数,主要是判断字符出现的次数,有需要的朋友可以参考一下";
$res = strrchr($str,&#39;主要&#39;);
echo $res;
echo "<br />";
$urlcode1 = urlencode("主");
$urlcode2 = urlencode("一");
echo "主的urlencode:".$urlcode1;
echo "<br />";
echo "一的urlencode:".$urlcode2;
echo "<br />";

echo "下的urlencode:".urlencode("下");
Copy after login

输出:


主的urlencode:%E4%B8%BB
一的urlencode:%E4%B8%80
下的urlencode:%E4%B8%8B
strrchr在中文比较的是将中文转化为urlencode后的第一个字符,只要相同就输出它到末尾的内容。。

因为:在主 和 下的  第一个字符,都是 %E4,那么他们相等,输出后面的内容


The above is the detailed content of PHP function strrchr() to find the last occurrence of a string in another string. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Return value: Returns a string from a certain string in another All characters from the last occurrence of the string to the end of the main string. If the character is not found, returns FALSE.
PHP Version: 4+
##Update Log :