function countWhitespaceCharacters($str) {
$count = 0;
$length = strlen($str);
for ($i = 0; $i < $length; $i++) {
if (ctype_space($str[$i])) {
$count++;
}
}
return $count;
}
$str = "PHP \n Programming \r Language \t ";
echo "Total white spaces: " . countWhitespaceCharacters($str);
/*
run:
Total white spaces: 10
*/