Skip to content

Instantly share code, notes, and snippets.

@notacouch
Created October 25, 2012 18:56
Show Gist options
  • Save notacouch/3954691 to your computer and use it in GitHub Desktop.
Save notacouch/3954691 to your computer and use it in GitHub Desktop.
Count occurrences of needle (substring) in haystack (string)
<?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));
@Neo1981
Copy link

Neo1981 commented May 2, 2013

very handy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment