CoderZone.org

Category: >> PHP Code >> Count Uppercase Letters In A String Bookmark and Share

<< lastnext >>

Snippet Name: Count Uppercase Letters In A String

Description: Easily count the number of uppercase chars in a string. Useful in forum postings and guestbooks to prevent "shouting".

Also see:
» Create an array from a range of le...
» Time left In Weeks, Days, Hours, A...
» Pre-populated List of Countries
» Count capital letters in a string
» Update/convert DB field to upperca...
» Count words in string

Comment: (none)

Author: CoderZone
Language: PHP
Highlight Mode: PHP
Last Modified: March 25th, 2010

// $test_string: the variable containing the string to do the search on 
// $caps_count: the variable that stores the count of uppercase chars 
// $caps_match: the array that stores all the regexp matches 
 
$test_string = "This Is My SPECIAL Test String"; 
preg_match_all("/[A-Z]/", $test_string, $caps_match); 
$caps_count = count($caps_match [0]); 
 
print "Uppercase Count: $caps_count"; 
 
// This will print "Uppercase Count: 12"
 
Add a comment 
MeMay 2nd, 2013 12:09pm
Useful snippet, thanks. Although the uppercase count should be 12.

Admin: thanks for catching that, it's been corrected.
© coderzone.org | users online: 10