2010-11-18 15 views
32

Jaki jest najlepszy sposób sprawdzenia, czy ciąg zawiera wszystkie wielkie litery? Nadal chcę, aby funkcja zwracała wartość true, jeśli ciąg zawiera również symbole lub liczby.Sprawdź, czy ciąg znaków jest CAŁKOWITĄ liczbą liter w PHP

+0

Może się okazać [ 's ($ str) -> isUpperCase()'] (https://github.com/delight-im/PHP-Str/blob/ea3e40132e9d4ce27da337dae6286f2478b15f56/src/Str.php# L262) pomocne, jak znaleźć w [tej samodzielnej bibliotece] (https://github.com/delight-im/PHP-Str). – caw

+0

Zobacz poniżej moje rozwiązanie - nie jest wymagane żadne regex ani funkcje. –

Odpowiedz

99

sprawdzić, czy strtoupper($str) == $str

Aby obsługiwać użycie non-aSCII:

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

+8

eleganckie rozwiązanie :) –

+4

Proste i słodkie! Podobnie jak legenda, że ​​Rosjanie używali ołówka w kosmosie. – Kumar

+0

Nice. Dobra robota. – Laizer

5

Jeśli chcesz numerów uwzględnionych (i przez „symbole” najbardziej wszystko inne), to co w rzeczywistości próbuje przetestować jest nieobecność z małych liter:

$all_upper = !preg_match("/[a-z]/", $string) 
1

PCRE:

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

prostu upewnij się, że nie korzystają z 'ja' modyfikator

1
if(mb_strtoupper($string)===$string) 
{ 
    do the required task 
} 
else 
{ 
    some other task 
} 
+2

Musisz podać "utf-8" jako drugi argument, tj.mb_strtoupper ("øæåØÆÅabcABC", "utf-8"); –

11
+0

Ponieważ ta odpowiedź przykuwa uwagę, warto zauważyć, że nie odpowiada ona poprawnie na pytanie, ponieważ nie uwzględnia numerów, zgodnie z żądaniem autora. – JCM

+1

Należy pamiętać, że funkcje Ctype są częścią "Rozszerzeń zmiennych i typów". Należy również zauważyć, że wbudowane wsparcie dla ctype jest dostępne od czasu 'PHP 4.3.0'. –

+0

ctype_upper zwróci wartość false, jeśli ciąg zawiera numer. Zobacz moje rozwiązanie poniżej. –

1

myślę szukasz to funkcja

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

Nadzieja pomaga

+1

Należy pamiętać, że funkcje Ctype są częścią "Rozszerzeń zmiennych i typów". Należy również zauważyć, że wbudowane wsparcie dla ctype jest dostępne od czasu 'PHP 4.3.0'. –

0

Oprócz komentarza Alexander Morland w sprawie odpowiedzi Winstona Ewert, jeśli trzeba radzić sobie z UTF-8 znaków akcentowanych można użyć następujący zestaw funkcji:

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

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

0

Tak radzę sobie z tekstem WSZYSTKICH CZAPEK dla mojej sekcji komentarzy.

if ($str == strtoupper($str)) 
{ 
    $str = ucfirst(strtolower($str)); 
} 
Powiązane problemy