is there an easy way to count uppercase words within a string?
You could use a regular expression to find all uppercase words and count them:
echo preg_match_all('/\b[A-Z]+\b/', $str);
The expression \b is a word boundary so it will only match whole uppercase words.
Shooting from the hip, but this (or something like it) should work:
function countUppercase($string) {
return preg_match_all(/\b[A-Z][A-Za-z0-9]+\b/, $string)
}
countUppercase("Hello good Sir"); // 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With