substr_count
Description
The substr_count of String for PHP count the number of substring occurrences.
Syntax
substr_count( string $haystack, string $needle, int $offset = 0, ?int $length = null ): int
Parameters
haystack
The string to search in
needle
The substring to search for
offset
The offset where to start counting. If the offset is negative, counting starts from the end of the string.
length
The maximum length after the specified offset to search for the substring. It outputs a warning if the offset plus the length is greater than the haystack length. A negative length counts from the end of haystack.
Return
Returns an int.
Examples
1 · haystack needle
<? $haystack = "abcdeabcdeabcde"; $needle = "abcde"; $return = substr_count($haystack, $needle); echo $return; ?>
3
2 · offset · negative
<? $haystack = "abcdeabcdeabcde"; $needle = "abcde"; $offset = -5; $return = substr_count($haystack, $needle, $offset); echo $return; ?>
1
3 · offset · non-negative
<? $haystack = "abcdeabcdeabcde"; $needle = "abcde"; $offset = 5; $return = substr_count($haystack, $needle, $offset); echo $return; ?>
2
4 · length · negative
<? $haystack = "abcdeabcdeabcde"; $needle = "abcde"; $offset = 5; $length = -5; $return = substr_count($haystack, $needle, $offset, $length); echo $return; ?>
1
5 · length · non-negative
<? $haystack = "abcdeabcdeabcde"; $needle = "abcde"; $offset = 5; $length = 5; $return = substr_count($haystack, $needle, $offset, $length); echo $return; ?>
1
Links
String
- addcslashes
- addslashes
- bin2hex
- chop
- chr
- chunk_split
- convert_uudecode
- convert_uuencode
- count_chars
- crc32
- crypt
- echo
- explode
- fprintf
- get_html_translation_table
- hebrev
- hebrevc
- hex2bin
- html_entity_decode
- htmlentities
- htmlspecialchars
- htmlspecialchars_decode
- implode
- join
- lcfirst
- levenshtein
- localeconv
- ltrim
- md5
- md5_file
- metaphone
- nl_langinfo
- nl2br
- number_format
- ord
- parse_str
- printf
- quoted_printable_decode
- quoted_printable_encode
- quotemeta
- rtrim
- setlocale
- sha1
- sha1_file
- similar_text
- soundex
- sprintf
- sscanf
- str_contains
- str_decrement
- str_ends_with
- str_getcsv
- str_increment
- str_ireplace
- str_pad
- str_repeat
- str_replace
- str_rot13
- str_shuffle
- str_split
- str_starts_with
- str_word_count
- strcasecmp
- strchr
- strcmp
- strcoll
- strcspn
- strip_tags
- stripcslashes
- stripos
- stripslashes
- stristr
- strlen
- strnatcasecmp
- strnatcmp
- strncasecmp
- strncmp
- strpbrk
- strpos
- strrchr
- strrev
- strripos
- strrpos
- strspn
- strstr
- strtok
- strtolower
- strtoupper
- strtr
- substr
- substr_compare
- substr_replace
- trim
- ucfirst
- ucwords
- vfprintf
- vprintf
- vsprintf
- wordwrap