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 - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- python 3x - typeerror: 'method' object is not subscriptable
- i want to call a child component method from a parent in vuejs
- xcode - pod install [!] no `podfile 'found in the project directory
- dart - flutter: the instance member'stars' can't be accessed in an initializer error