Friday, 19 September 2014

PHP function to check string if is all lowercase



ctype_lower:
This can be used to check for lowercase character(s)

Syntax:
bool ctype_lower ( string $text )

Parameter:
text:The tested string.

Return value:
Returns TRUE if every character in text is a lowercase in the current locale.


<?php/* simple code shippet for check_lowercase_string
function check_lowercase_string($string) {
    return ($string === strtolower($string));
}
*/


//created function with full analysis(it is created instead of built in function.)
function check_lowercase_string($string) {
    $chars = '';
    // map all small chars
    for($alpha = 'a'; $alpha != 'aa'; $alpha++) { $small[] = $alpha; }
    $l = 0; // not strlen() :p
    while (@$string[$l] != '') {
        $l++;
    }
    for($i = 0; $i < $l; $i++) { // for each string input piece
        foreach($small as $letter) { // for each mapped letter
            if($string[$i] == $letter) {
                $chars .= $letter; // simple filter
            }
        }
    }

    // if they are still equal in the end then true, if they are not, false
    return ($chars === $string);
}
 





$string = 'Hi! I am a string';
var_dump(check_lowercase_string($string));  //false
$string = 'string';
var_dump(check_lowercase_string($string));  //true

?>

0 comments:

Post a Comment