Finding the number of occurrences of a string in PHP can be done by using the Substr_count () function, and I'll give you a detailed introduction to these functions.
Substr_count ($haystack, $needle [, $offset [, $length]])
/$haystack represents a female string, $needl represents the character to find
$offset represents the starting point for the lookup, $length the length of the lookup, which is an optional parameter
Instance:
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). ' '; ?> |
Instance:
The code is as follows |
Copy Code |
$str = ' Http://www.bkjia.com Love program Net _ Software Programming Tutorial _ Software Design Communication _ character occurrences '; Echo Substr_count ($str, ' W '). ' '; Echo Substr_count ($str, ' t '). ' '; Echo Substr_count ($str, ' Love program Net '); ?> The output is: Here are the output results 3 2 1 |
Share some string lookup functions again
strstr-finding the first occurrence of a string
Stristr strstr case-insensitive version
Strpos-Find where the string first appears
String substr (string $string, int $start [, int $length])
String STRRCHR (String $haystack, mixed $needle)
Strripos-Calculates the location of the last occurrence of the specified string in the target string (case insensitive)
Stripos-Find where the string first appears (not case-sensitive)
Strrpos-Calculates the position of the last occurrence of the specified string in the target string
http://www.bkjia.com/PHPjc/445621.html www.bkjia.com true http://www.bkjia.com/PHPjc/445621.html techarticle Finding The number of occurrences of a string in PHP can be done by using the Substr_count () function, and I'll give you a detailed introduction to these functions. Substr_count ($haystack, $needle [,...