本文共 1374 字,大约阅读时间需要 4 分钟。
substr_count
substr_count() function is a string function in PHP, it is used to find the total number of occurrences of a substring in the given string.
substr_count()函数是PHP中的字符串函数,用于查找给定字符串中子字符串出现的总数。
Syntax:
句法:
substr_count(string, substring, [offset], [length]);
Here,
这里,
string is the main string in which we have to perform the search for the substring.
string是我们必须在其中搜索子 字符串的主要字符串。
substring is the part of the string that we have search in the string.
substring是我们在字符串中搜索的字符串的一部分 。
offset is an optional parameter, it defines the start index – from where you want to search the string. Its default value is 0.
offset是一个可选参数,它定义了起始索引,即您要从中搜索字符串的位置。 默认值为0。
length is also an optional parameter, it defines length of the search i.e. search operation will start from offset and finishes with offset+length position.
length也是一个可选参数,它定义了搜索的长度,即搜索操作将从offset开始,并以offset + length位置结束。
Examples:
例子:
Input: str = "Hello friends how are your friends?"; substring = "friends"; Output: 2 Input: str = "Hello friends how are your friends?"; substring = "Hello"; Output: 1 Input: str = "Hello friends how are your friends?"; substring = "Hi"; Output: 0
PHP code:
PHP代码:
Output
输出量
friends found 2 times.Hi found 0 times.friends found 1 times.friends found 0 times.
翻译自:
substr_count
转载地址:http://gnvzd.baihongyu.com/