Check if string contains uppercase php

preg_match_all('%\p{Lu}%usD', 'aA,éÁ,eE,éÉ,iI,íÍ,oO,óÓ,öÖ,őŐ,uU,úÚ,üÜ,űŰ', $m);
echo '<pre>';
var_dump($m);
echo '</pre>';

Tested with hungarian utf-8 characters, [A-Z] is for latin1 only.

We have seen how to check for all the upper case letters or all lower case letters ( or special chars ) presence in a string variable. However we may need a situation where we have to ask the user (while entering password ) to enter at least one lower case letter or one upper case letter or one special characters.

We will check one by one first and then develop a script to combine all these points.

This code will be useful for checking at server side ( not client side ) password entered by user.

Check for at least one upper case character

We used preg_match which returns True of False after checking the string.
$string='rtAF78a';
if(preg_match('/[A-Z]/', $string)){
echo " There is at least  one Upper Case Characters inside the string ";
}else{
echo " There is no Upper case characters inside the string ";
}
The output is
There is at least one Upper Case Characters inside the string 

Check for at lease one lower case character

if(preg_match('/[a-z]/', $string)){
echo " There is at least one lower Case Characters inside the string ";
}else{
echo " There is no lower case characters inside ";
}
Output is
There is at least one lower Case Characters inside the string
We will use string search function strstr() to search for presence of blank string inside.

$string='rtAF 78a';
if (strstr($string,' ')) {
echo " There is at least one blank space present inside the string ";
}else{
echo " There is NO blank space present inside the string ";
}
Output is
There is blank space present inside the string 

Check for special characters

if (preg_match('/[\'^�$%&*()}{@#~?><>,|=_+�-]/', $string)) {
echo " There is at least one special characters  present inside the string";
}else{
echo " There is no special characters present in the string ";
}
There is no special characters present in the string 

Check if string contains uppercase php


(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)

<?php
$strings 
= array('AKLWC139''LMNSDO''akwSKWsm');
foreach (
$strings as $testcase) {
    if (
ctype_upper($testcase)) {
        echo 
"The string $testcase consists of all uppercase letters.\n";
    } else {
        echo 
"The string $testcase does not consist of all uppercase letters.\n";
    }
}
?>

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 check if string is uppercase in PHP?

The ctype_upper() function in PHP used to check each and every character of a given string is in uppercase or not. If the string in upper case then it returns TRUE otherwise returns False.

How do I check if a string contains uppercase?

Below are the steps:.
Traverse the string character by character from start to end..
Check the ASCII value of each character for the following conditions: If the ASCII value lies in the range of [65, 90], then it is an uppercase letter. ... .
Print Yes if the string contains all the above. Otherwise, print No..

How do you check if there is a capital letter in a string Java?

isUpperCase(char ch) determines if the specified character is an uppercase character.

How do you check if a string has an uppercase letter python?

The string isupper() method returns True if all cased characters in a string are uppercase. Otherwise, it returns False . If the string doesn't contain any cased characters, the isupper() method also returns False .