Use this:
substr_count($text, ' ');
You're looking for preg_match_all.
preg_match_all
$numSpaces = preg_match_all('/[ ]/', $testStr, $matches);
$arr = count_chars($str,1);echo $arr[32];