Home  >  Article  >  Backend Development  >  PHP's four functions for finding strings in strings (strstr(), strchr(), strrchr(), stristr())

PHP's four functions for finding strings in strings (strstr(), strchr(), strrchr(), stristr())

黄舟
黄舟Original
2017-11-01 09:30:551643browse

To find another string in a string you can use strstr(),strchr(),strrchr(),stristr() Any one of the four functions.

The function strstr() is the most common. The function prototype is:

strstr(string,search,before_search);
//string要被搜索的字符串,search所搜索的字符串,before_search默认值为 "false" 的布尔值,如果设置为 "true",它将返回 search 参数第一次出现之前的字符串部分。

Example:

echo strstr("I love Shanghai","love");//返回值为“love Shanghai”

The function strchr() is exactly the same as the function strstr();

The function stristr() is almost the same as the function strstr(). The difference between them is that the target keyword string of strstr() is case-sensitive, while the target keyword string of stristr() is not case-sensitive;

The function strrchr() is almost the same as the function strstr(), but strrchr() will return the searched string from before the last occurrence of the target keyword.

The above is the detailed content of PHP's four functions for finding strings in strings (strstr(), strchr(), strrchr(), stristr()). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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