Skip to content

Instantly share code, notes, and snippets.

@cdrubin
Last active April 23, 2020 14:33
Show Gist options
  • Select an option

  • Save cdrubin/b9d7a3d16ac67546ff5f0d270774de7c to your computer and use it in GitHub Desktop.

Select an option

Save cdrubin/b9d7a3d16ac67546ff5f0d270774de7c to your computer and use it in GitHub Desktop.
shared
function count_instances_of_word_in_string( $word, $string ) {
$found = 0;
$matches = preg_match( $word, $string );
return count( $matches );
}
$paragraph = <<<EOP
This is some text and I expect there will be some
some words that appear more than once. Especially if
I use the pronoun I more than once and even the word
more will appear more than once at this point.
EOP;
echo count_instances_of_word_in_string( 'more', $paragraph );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment