```php function count_capitals($str) { return strlen(preg_replace('![^A-Z]+!', '', $str)); } ```