<?php
	function NumInstancesOfWord($HayStack, $Needle, $MatchCase = false)
	{
	  $WordArray = array();
		# Make shure masty chars don't affect the results.
		$HayStack  = preg_replace('#[^a-z0-9 ]#si', '', $HayStack);
	  if ($MatchCase == false) # Case insensative
		{
	    $WordArray = explode(' ', strtolower($HayStack));
			$$Needle   = strtolower($Needle);
	  }
		else
		{
	    $WordArray = explode(' ', $HayStack);
		}
		
		$CountVar = 0;
		$i        = 0;
		while ($WordArray[$i])
		{
		  if ($WordArray[$i] == $Needle)
			{
			  ++$CountVar;
			}
		  ++$i;
		}
		return ($CountVar);
	}
	$TestStr = 'This is a test, testing 123, testing 1123';
	
	echo ('There are ' . NumInstancesOfWord($TestStr, 'testing') . ' instances of testing.<br />');
	echo ('There are ' . NumInstancesOfWord($TestStr, 'test')    . ' instances of test.<br />');
	echo ($TestStr);
?>