Created
October 25, 2012 18:56
-
-
Save notacouch/3954691 to your computer and use it in GitHub Desktop.
Count occurrences of needle (substring) in haystack (string)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Just thought this was really cool and fast way to count occurrences of a string within a string | |
// @author Thai | |
// @link http://stackoverflow.com/a/4736219/781824 | |
$needle = 'AGUXYZ'; | |
$haystack = 'Agriculture ID XYZ-A'; | |
$count = strlen($haystack) - strlen(str_replace(str_split($needle), '', $haystack)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
very handy!