How check if string is uppercase in php?

In addition to Alexander Morland's comment on Winston Ewert's answer if you need to deal with utf-8 accented characters you can use the following set of functions:

define['CHARSET', 'utf-8'];

function custom_substr[$content='',$pos_start=0,$num_char=1]{
    $substr='';
    if[function_exists['mb_substr']]{
        $substr=mb_substr[$content,$pos_start,$num_char,CHARSET];
    }
    else{
        $substr=substr[$content,$pos_start,$num_char];
    }
    return $substr;
}
function custom_str_case[$string='', $case='lower']{
    $lower = array[
        "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
        "v", "w", "x", "y", "z", "à", "á", "â", "ã", "ä", "å", "æ", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï",
        "ð", "ñ", "ò", "ó", "ô", "õ", "ö", "ø", "ù", "ú", "û", "ü", "ý", "а", "б", "в", "г", "д", "е", "ё", "ж",
        "з", "и", "й", "к", "л", "м", "н", "о", "п", "р", "с", "т", "у", "ф", "х", "ц", "ч", "ш", "щ", "ъ", "ы",
        "ь", "э", "ю", "я"
    ];
    $upper = array[
        "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
        "V", "W", "X", "Y", "Z", "À", "Á", "Â", "Ã", "Ä", "Å", "Æ", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï",
        "Ð", "Ñ", "Ò", "Ó", "Ô", "Õ", "Ö", "Ø", "Ù", "Ú", "Û", "Ü", "Ý", "А", "Б", "В", "Г", "Д", "Е", "Ё", "Ж",
        "З", "И", "Й", "К", "Л", "М", "Н", "О", "П", "Р", "С", "Т", "У", "Ф", "Х", "Ц", "Ч", "Ш", "Щ", "Ъ", "Ъ",
        "Ь", "Э", "Ю", "Я"
    ];

    if[$case=='lower']{
        $string = str_replace[$upper, $lower, $string];
    }
    else{
        $string = str_replace[$lower, $upper, $string];
    }

    return $string;
}
function custom_strtolower[$string]{
    return custom_str_case[$string,'lower'];
}
function custom_strtoupper[$string]{
    return custom_str_case[$string,'upper'];
}

function custom_ucfirst[$string]{
    $string=custom_strtolower[$string];

    $first_char=custom_substr[$string,0,1];
    $rest_char=custom_substr[$string,1,custom_strlen[$string]];
    $first_char=custom_strtoupper[$first_char];

    return $first_char.$rest_char;
}

function is_uppercase[$string='']{
    $is_uppercase=false;

    if[$string === custom_strtoupper[$string]] {
       $is_uppercase=true;
    }

    return $is_uppercase;
}
function is_ucfirst[$string='']{
    $first_char=custom_substr[$string,0,1];

    $is_ucfirst=is_uppercase[$first_char];

    return $is_ucfirst;
}

Resources:: //github.com/rafasashi/PHP-Custom-String-Functions

[PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8]

ctype_upperCheck for uppercase character[s]

Description

ctype_upper[mixed $text]: bool

Parameters

text

The tested string.

Note:

If an int between -128 and 255 inclusive is provided, it is interpreted as the ASCII value of a single character [negative values have 256 added in order to allow characters in the Extended ASCII range]. Any other integer is interpreted as a string containing the decimal digits of the integer.

Warning

As of PHP 8.1.0, passing a non-string argument is deprecated. In the future, the argument will be interpreted as a string instead of an ASCII codepoint. Depending on the intended behavior, the argument should either be cast to string or an explicit call to chr[] should be made.

Return Values

Returns true if every character in text is an uppercase letter in the current locale. When called with an empty string the result will always be false.

Examples

Example #1 A ctype_upper[] example [using the default locale]

The above example will output:

The string AKLWC139 does not consist of all uppercase letters.
The string LMNSDO consists of all uppercase letters.
The string akwSKWsm does not consist of all uppercase letters.

See Also

  • ctype_alpha[] - Check for alphabetic character[s]
  • ctype_lower[] - Check for lowercase character[s]
  • setlocale[] - Set locale information

How do you check if a string is uppercase?

To check if a letter in a string is uppercase or lowercase use the toUpperCase[] method to convert the letter to uppercase and compare it to itself. If the comparison returns true , then the letter is uppercase, otherwise it's lowercase. Copied!

How do you check if a string contains a special character in PHP?

php function check_string[$my_string]{ $regex = preg_match['[@_! #$%^&*[]?/|}{~:]', $my_string]; if[$regex] print["String has been accepted"]; else print["String has not been accepted"]; } $my_string = 'This_is_$_sample!

How do you capitalize text in PHP?

The ucfirst[] function converts the first character of a string to uppercase. Related functions: lcfirst[] - converts the first character of a string to lowercase. ucwords[] - converts the first character of each word in a string to uppercase.

How do you check if a string contains a number in PHP?

The is_numeric[] function checks whether a variable is a number or a numeric string. This function returns true [1] if the variable is a number or a numeric string, otherwise it returns false/nothing.

Chủ Đề