Thursday, February 24, 2011

is_lower() and is_upper()

In this post i will share my functions is_lower and is_upper. I coded them for determinening is string lower case or upper case. Idea is simple. Get each char with substring, after that convert it to ascii (with ord function) and in condition clause look for its range (97-122 lower case chars and 65-90 upper case chars). Here is functions..

is_lower:
function is_lower($string) {
for ($i = 0; $i < strlen($string); $i++) {
$ch = ord(substr($string, $i, 1));
if (!(($ch > 96) && ($ch < 123))) {
return false;
}
}

return true;
}

is_upper:
function is_upper($string) {
for ($i = 0; $i < strlen($string); $i++) {
$ch = ord(substr($string, $i, 1));
if (!(($ch > 64) && $ch < 91)) {
return false;
}
}

return true;
}

No comments:

Post a Comment