Substr_count () in PHP can be used to count the number of times a substring appears in a specified string.
The substr_count () function is defined as follows:
substr_count (string, substring, start, length)
string Required. Specifies the string to be checked
substring Required. Specifies the string to search.
start Optional. Specifies where to start the search in the string.
length Optional. Specifies the length of the search.
Sample codeThe following:
<?php echo substr_count ($str, "script"); echo "<br />"; echo substr_count ($str, "script", 16);//Specify that the search starts after the 16th character echo "<br />"; echo substr_count ($str, "script", 16,10);//Specify the search starting from the 16th character and ending with 10 characters ?>
The results are as follows:
3 2 1
|string||essential. Specifies the string to be checked|
|substring||essential. Specifies the string to search.|
|start||Optional. Specifies where to start the search in the string.|
|length||Optional. Specifies the length of the search.|
|return value:||Returns the number of times a substring appears in a string.|
|Update log:||In php 5.1, the start and length parameters were added.|
Use all parameters:
<?php $str="this is nice"; echo strlen ($str). "<br>";//use strlen () to return the length of the string echo substr_count ($str, "is"). "<br>";//the number of times "is" appears in the string echo substr_count ($str, "is", 2). "<br>";//string is reduced to "is is nice" echo substr_count ($str, "is", 3). "<br>";//string is reduced to "s is nice" echo substr_count ($str, "is", 3,3). "<br>";//string is reduced to "s i" ?>
<?php $str="abcabcab"; echo substr_count ($str, "abcab");//this function does not count overlapping substrings ?>
If the start and length parameters exceed the string length,The function will output a warning:
<?php echo $str="this is nice"; substr_count ($str, "is", 3,9); ?>
Because the length value exceeds the length of the string (3 + 9 is greater than 12), use will output a warning.
- php get first few instances of string (substr returns string substring usage)
- PHP implementation of the method to find the longest common substring of two strings
- Summary of methods for inserting substrings in PHP strings
- PHP implements a method to find substrings in a specified string
- PHP method to get substring based on specified position and length
- PHP implementation of substring position mutual swap method
- PHP using strpos to determine a numeric type substring in a string is wrong
- Example analysis of extracting substrings from PHP strings
- python 3x - typeerror: 'method' object is not subscriptable
- python - you may need to restart the kernel to use updated packages error
- xcode - pod install [!] no `podfile 'found in the project directory
- android studio - unresolved reference comes out in kotlin
- vuejs - [vuetify] unable to locate target [data-app] i want to unit test to avoid warning
- android studio - emulator: dsound: could not initialize about the error message directsoundcapture
- django - oserror: [winerror 123] the file name, directory name, or volume label syntax is incorrect : '<frozen importlib_boot
- mysql startup failed [error] innodb: the innodb_system data file 'ibdata1' must be writable
- python - importerror: cannot import name md5 error cannot be resolved