How to use Built-in Functions In PHP
PHP provides several built-in functions that help determine the presence of specific character types in a string.
<?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 = "w3wiki123@#$";
$result = checkChars($str);
foreach ($result as $type => $hasType) {
echo "$type: " . ($hasType ? 'Yes' : 'No') . "\n";
}
?>
Output
Uppercase: Yes Lowercase: Yes Special Characters: Yes Numeric Values: Yes
PHP Program to Check if a String Contains Uppercase, Lowercase, Special Characters and Numeric Values
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 = "w3wiki123@#$" 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 = "w3wiki" Output: No Explanation: The given string contains only uppercase characters and lowercase characters. Therefore, the output is No.
here are some common approaches:
Table of Content
- Using Built-in Functions
- Using Loop and ctype Functions
- Using filter_var and Custom Validation
Contact Us