Searching for the number of occurrences of a string in PHP can be achieved through the substr_count() function. Let me introduce these functions to you in detail.
substr_count($haystack, $needle [,$offset [,$length]])
/$haystack represents the parent string, $needl represents the character to be found
//$offset represents the starting point of the search, $length represents the length of the search, both are optional parameters
Example:
The code is as follows |
Copy code |
代码如下 |
复制代码 |
$str="this is a test";
echo substr_count($str,'is') .'
';
echo substr_count($str,'is',3) .'
';
echo substr_count($str,'is',3,3) .'
';
?>
|
$str="this is a test";
echo substr_count($str,'is') .'
';
echo substr_count($str,'is',3) .'
代码如下 |
复制代码 |
$str = 'http://www.bkjia.com 爱程序网_软件编程入门教程_软件设计交流_字符出现次数';
echo substr_count($str,'w').'
';
echo substr_count($str,'t').'
';
echo substr_count($str,'爱程序网');
?>
输出结果为:
//以下是输出结果
3
2
1
|
';
echo substr_count($str,'is',3,3) .'
';
?>
|
Example:
The code is as follows |
Copy code |
$str = 'http://www.bkjia.com AiProgram.com_Software Programming Introduction Tutorial_Software Design Communication_Number of Character Occurrences';
echo substr_count($str,'w').'
';
echo substr_count($str,'t').'
';
echo substr_count($str,'爱program.com');
?>
The output result is:
//The following is the output result
3
2
1
|
Share some more string search functions
strstr — Find the first occurrence of a string
stristr strstr case-insensitive version
strpos - Find the first occurrence of a string
string substr ( string $string , int $start [, int $length ] )
string strrchr ( string $haystack , mixed $needle )
strripos - calculates the position of the last occurrence of the specified string in the target string (case-insensitive)
stripos - Find the first occurrence of a string (insensitive)
http://www.bkjia.com/PHPjc/445621.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445621.htmlTechArticleFinding the number of occurrences of a string in php can be achieved through the substr_count() function. Let me tell you about it below The students introduced these functions in detail. substr_count($haystack, $needle [,...
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