Vérifiez si une chaîne est en majuscules en PHP

Quelle est la meilleure façon de voir si une chaîne contient toutes les lettres majuscules? Je veux encore la fonction retourne true si la chaîne contient également des symboles ou des chiffres.

32
demandé sur Kirk Ouimet 2010-11-18 08:09:56

9 réponses

Vérifier si strtoupper($str) == $str

Pour gérer l'utilisation non-ascii:

mb_strtoupper($str, 'utf-8') == $str

105
répondu Winston Ewert 2013-11-29 17:17:47
11
répondu JCM 2011-09-12 00:44:36

Si vous voulez des nombres inclus (et par" Symboles " la plupart du reste), alors ce que vous essayez réellement de tester est l'absence de lettres minuscules:

 $all_upper = !preg_match("/[a-z]/", $string)
5
répondu mario 2010-11-18 05:15:20

Vous pouvez utiliser preg_match(). L'expression régulière serait /^[^a-z]+$/.

return preg_match('/^[^a-z]+$/', $string) === 1 ? true : false;

Voici la documentation de preg_match().

Http://php.net/manual/en/function.preg-match.php

1
répondu Bill Criswell 2010-11-18 05:16:20

Solution PCRE:

$all_uppercase = preg_match('#^[A-Z]+$#', $string);

Assurez-vous simplement de ne pas utiliser le modificateur ' i '

1
répondu iroel 2010-11-18 09:28:45
if(mb_strtoupper($string)===$string)
{
  do the required task
}
else
{
  some other task
}
1
répondu Kracekumar 2010-11-19 12:35:57

Je pense que vous recherchez cette fonction

$myString = "ABCDE";
if (ctype_upper($myString)) // returns true if is fully uppercase
{
    echo "the string $myString is fully uppercase";
}

J'espère que ça aide

1
répondu Gabi Dj 2013-03-29 13:08:09

En plus du commentaire D'Alexander Morland sur la réponse de Winston Ewert si vous avez besoin de traiter les caractères accentués utf-8, Vous pouvez utiliser l'ensemble de fonctions suivant:

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;
}

Ressources:: https://github.com/rafasashi/PHP-Custom-String-Functions

0
répondu RafaSashi 2015-01-02 09:15:39

Voici comment je gère le texte en majuscules pour ma section de commentaires.

if ($str == strtoupper($str))
{
    $str = ucfirst(strtolower($str));
}
0
répondu Brian Smith 2017-06-26 03:26:46