PHP Program to Check if a String Contains Uppercase, Lowercase, Special Characters and Numeric Values
Last Updated :
05 Jun, 2024
Given a String, the task is to check whether the given string contains uppercase, lowercase, special characters, and numeric values in PHP. When working with strings in PHP, it's often necessary to determine the presence of certain character types within the string, such as uppercase letters, lowercase letters, special characters, and numeric values.
Examples:
Input: str = "GeeksforGeeks123@#$"
Output: Yes
Explanation: The given string contains uppercase
characters('G', 'F'), lowercase characters('e', 'k', 's', 'o', 'r'),
special characters( '#', '@'), and numeric values('1', '2', '3').
Therefore, the output is Yes.
Input: str = "GeeksforGeeks"
Output: No
Explanation: The given string contains only uppercase
characters and lowercase characters. Therefore, the
output is No.
here are some common approaches:
Using Built-in Functions
PHP provides several built-in functions that help determine the presence of specific character types in a string.
PHP
<?php
function checkChars($str) {
$upperCase = preg_match('/[A-Z]/', $str);
$lowerCase = preg_match('/[a-z]/', $str);
$specialChar = preg_match('/[^A-Za-z0-9]/', $str);
$numericVal = preg_match('/[0-9]/', $str);
return [
'Uppercase' => $upperCase,
'Lowercase' => $lowerCase,
'Special Characters' => $specialChar,
'Numeric Values' => $numericVal,
];
}
// Driver code
$str = "GeeksforGeeks123@#$";
$result = checkChars($str);
foreach ($result as $type => $hasType) {
echo "$type: " . ($hasType ? 'Yes' : 'No') . "\n";
}
?>
OutputUppercase: Yes
Lowercase: Yes
Special Characters: Yes
Numeric Values: Yes
Using Loop and ctype Functions
Another approach involves iterating through each character in the string and using ctype functions to check their types.
PHP
<?php
function checkChars($str) {
$upperCase = $lowerCase = $specialChar = $numericVal = false;
for ($i = 0; $i < strlen($str); $i++) {
if (ctype_upper($str[$i])) {
$upperCase = true;
} else if (ctype_lower($str[$i])) {
$lowerCase = true;
} else if (ctype_digit($str[$i])) {
$numericVal = true;
} else {
$specialChar = true;
}
}
return [
'Uppercase' => $upperCase,
'Lowercase' => $lowerCase,
'Special Characters' => $specialChar,
'Numeric Values' => $numericVal,
];
}
// Driver code
$str = "GeeksforGeeks123@#$";
$result = checkChars($str);
foreach ($result as $type => $hasType) {
echo "$type: " . ($hasType ? 'Yes' : 'No') . "\n";
}
?>
OutputUppercase: Yes
Lowercase: Yes
Special Characters: Yes
Numeric Values: Yes
Using filter_var and Custom Validation
This approach uses filter_var in combination with custom validation logic to check for the presence of uppercase, lowercase, special characters, and numeric values.
PHP
<?php
// Nikunj Sonigara
function checkChars($str) {
$upperCase = $lowerCase = $specialChar = $numericVal = false;
$upperCase = filter_var($str, FILTER_VALIDATE_REGEXP,
array("options"=>array("regexp"=>"/[A-Z]/"))) !== false;
$lowerCase = filter_var($str, FILTER_VALIDATE_REGEXP,
array("options"=>array("regexp"=>"/[a-z]/"))) !== false;
$specialChar = filter_var($str, FILTER_VALIDATE_REGEXP,
array("options"=>array("regexp"=>"/[^A-Za-z0-9]/"))
) !== false;
$numericVal = filter_var($str, FILTER_VALIDATE_REGEXP,
array("options"=>array("regexp"=>"/[0-9]/"))) !== false;
return [
'Uppercase' => $upperCase,
'Lowercase' => $lowerCase,
'Special Characters' => $specialChar,
'Numeric Values' => $numericVal,
];
}
// Driver code
$str = "GeeksforGeeks123@#$";
$result = checkChars($str);
foreach ($result as $type => $hasType) {
echo "$type: " . ($hasType ? 'Yes' : 'No') . "\n";
}
?>
OutputUppercase: Yes
Lowercase: Yes
Special Characters: Yes
Numeric Values: Yes
Similar Reads
How to check if a String contains a Specific Character in PHP ? In PHP, determining whether a string contains a specific character is a common task. Whether you're validating user input, parsing data, or performing text processing, PHP provides several methods to check for the presence of a particular character within a string efficiently. Here are some common a
2 min read
PHP Program to Check Whether a Character is a Vowel or Consonant There are a total of 26 letters in the English alphabet. There are 5 vowel letters and 21 consonant letters. The 5 vowel letters are: "a", "e", "i", "o", and "u" rest are consonants. There are different approaches to checking whether a character is a vowel or a consonant. These are the different App
3 min read
How to convert lowercase string to uppercase using PHP ? A string is a storage object in PHP comprising characters, numbers or special symbols. Strings in PHP are case-sensitive. The interconversion between lower and upper case can be easily done using various in-built methods in PHP.Table of ContentUsing chr() method Using strtoupper() method Using mb_co
4 min read
How to convert uppercase string to lowercase using PHP ? Converting an uppercase string to lowercase means changing all capital letters in the string to their corresponding small letters. This is typically done using programming functions or methods to ensure uniformity in text formatting and comparison.Below we have some common approaches Table of Conten
2 min read
How to convert the first character to uppercase using PHP ? A string is a combination of words combined together. The first character of the string can be converted to an upper case in case it is a lower case alphabetic character. Approach 1 : Using chr() method Step 1: The first character of a string can be extracted using the first index, str[0] returns th
3 min read
Shell Script To Check That Input Only Contains Alphanumeric Characters If you want an input which contains only alphanumeric characters i.e. 1-9 or a-z lower case as well as upper case characters, We can make use of regular expression or Regex in short in a Shell Script to verify the input. Example: Input: Geeksforgeeks Output: True Explanation: Here all the inputted d
2 min read
How to check if a String Contains a Substring in PHP ? Checking whether a string contains a specific substring is a common task in PHP. Whether you're parsing user input, filtering content, or building search functionality, substring checking plays a crucial role.MethodsBelow are the following methods by which we can check if a string contains a substri
1 min read
How to convert first character of all the words uppercase using PHP ? To convert the first character of all the words present in a string to uppercase, we just need to use one PHP function i.e. ucwords().ucwords(string,delimiters)This function takes 2 parameters. The first one is the string which is mandatory. The second parameter is the delimiter. Example 1:PHP<?p
2 min read
Check if a given String is Binary String or Not using PHP Given a String, the task is to check whether the given string is a binary string or not in PHP. A binary string is a string that should only contain the '0' and '1' characters. Examples:Input: str = "10111001"Output: The string is binary.Input: str = "123456"Output: The string is not binary.Table of
5 min read