1--TEST-- 2Test substr_count() function (variation - 1) 3--FILE-- 4<?php 5 6echo "\n*** Testing possible variations ***\n"; 7echo "-- 3rd or 4th arg as string --\n"; 8$str = "this is a string"; 9var_dump( substr_count($str, "t", "5") ); 10var_dump( substr_count($str, "t", "5", "10") ); 11var_dump( substr_count($str, "i", "5t") ); 12var_dump( substr_count($str, "i", "5t", "10t") ); 13 14echo "\n-- 3rd or 4th arg as NULL --\n"; 15var_dump( substr_count($str, "t", "") ); 16var_dump( substr_count($str, "T", "") ); 17var_dump( substr_count($str, "t", "", 15) ); 18var_dump( substr_count($str, "I", NULL) ); 19var_dump( substr_count($str, "i", NULL, 10) ); 20 21echo "\n-- overlapped substrings --\n"; 22var_dump( substr_count("abcabcabcabcabc", "abca") ); 23var_dump( substr_count("abcabcabcabcabc", "abca", 2) ); 24 25echo "\n-- complex strings containing other than 7-bit chars --\n"; 26$str = chr(128).chr(129).chr(128).chr(256).chr(255).chr(254).chr(255); 27var_dump(substr_count($str, chr(128))); 28var_dump(substr_count($str, chr(255))); 29var_dump(substr_count($str, chr(256))); 30 31echo "\n-- heredoc string --\n"; 32$string = <<<EOD 33abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 34abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 35acdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 36acdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 37abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 38abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 39abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 40abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 41EOD; 42var_dump(substr_count($string, "abcd")); 43var_dump(substr_count($string, "1234")); 44 45echo "\n-- heredoc null string --\n"; 46$str = <<<EOD 47EOD; 48var_dump(substr_count($str, "\0")); 49var_dump(substr_count($str, "\x000")); 50var_dump(substr_count($str, "0")); 51 52echo "Done\n"; 53 54?> 55--EXPECTF-- 56*** Testing possible variations *** 57-- 3rd or 4th arg as string -- 58int(1) 59int(1) 60 61Notice: A non well formed numeric value encountered in %s on line %d 62int(2) 63 64Notice: A non well formed numeric value encountered in %s on line %d 65 66Notice: A non well formed numeric value encountered in %s on line %d 67int(2) 68 69-- 3rd or 4th arg as NULL -- 70 71Warning: substr_count() expects parameter 3 to be int, string given %s on line %d 72NULL 73 74Warning: substr_count() expects parameter 3 to be int, string given %s on line %d 75NULL 76 77Warning: substr_count() expects parameter 3 to be int, string given %s on line %d 78NULL 79int(0) 80int(2) 81 82-- overlapped substrings -- 83int(2) 84int(2) 85 86-- complex strings containing other than 7-bit chars -- 87int(2) 88int(2) 89int(1) 90 91-- heredoc string -- 92int(14) 93int(16) 94 95-- heredoc null string -- 96int(0) 97int(0) 98int(0) 99Done 100