|
substr_count
Count the number of substring occurrences
(PHP 4, PHP 5)
Example 2475. A substr_count() example<?php Related Examples ( Source code ) » substr_count Examples ( Source code ) » Substring count Code Examples / Notes » substr_countxinfox x
Yet another reference to the "cgcgcgcgcgcgc" example posted by "chris at pecoraro dot net": Your request can be fulfilled with the Perl compatible regular expressions and their lookahead and lookbehind features. The example $number_of_full_pattern = preg_match_all('/(cgc)/', "cgcgcgcgcgcgcg", $chunks); works like the substr_count function. The variable $number_of_full_pattern has the value 3, because the default behavior of Perl compatible regular expressions is to consume the characters of the string subject that were matched by the (sub)pattern. That is, the pointer will be moved to the end of the matched substring. But we can use the lookahead feature that disables the moving of the pointer: $number_of_full_pattern = preg_match_all('/(cg(?=c))/', "cgcgcgcgcgcgcg", $chunks); In this case the variable $number_of_full_pattern has the value 6. Firstly a string "cg" will be matched and the pointer will be moved to the end of this string. Then the regular expression looks ahead whether a 'c' can be matched. Despite of the occurence of the character 'c' the pointer is not moved. flobi
Making this case insensitive is easy for anyone who needs this. Simply convert the haystack and the needle to the same case (upper or lower). substr_count(strtoupper($haystack), strtoupper($needle)) danjr33
I ran into trouble using this function when I moved a script from a server with PHP5 to a server with only PHP4. As the last two parameters were added with 5.1.0, I wrote a substitute function: <?php function substr_count5($str,$search,$offset,$len) { return substr_count(substr($str,$offset,$len),$search); } ?> Use it exactly as substr_count() is used in PHP5. (This will work in PHP5 as well.) info
a simple version for an array needle (multiply sub-strings): <?php function substr_count_array( $haystack, $needle ) { $count = 0; foreach ($needle as $substring) { $count += substr_count( $haystack, $substring); } return $count; } ?> |
Change Languageaddcslashes addslashes bin2hex chop chr chunk_split convert_cyr_string convert_uudecode convert_uuencode count_chars crc32 crypt echo explode fprintf get_html_translation_table hebrev hebrevc html_entity_decode htmlentities htmlspecialchars_decode htmlspecialchars implode join levenshtein localeconv ltrim md5_file md5 metaphone money_format nl_langinfo nl2br number_format ord parse_str printf quoted_printable_decode quotemeta rtrim setlocale sha1_file sha1 similar_text soundex sprintf sscanf str_getcsv str_ireplace str_pad str_repeat str_replace str_rot13 str_shuffle str_split 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_compare substr_count substr_replace substr trim ucfirst ucwords vfprintf vprintf vsprintf wordwrap |