simplest, shortest way to count capital letters in a string with php?

pablo picture pablo · Oct 13, 2009 · Viewed 12.1k times · Source

I am looking for the shortest, simplest and most elegant way to count the number of capital letters in a given string.

Answer

cletus picture cletus · Oct 13, 2009
function count_capitals($s) {
  return mb_strlen(preg_replace('![^A-Z]+!', '', $s));
}