38. PHP Program to check given Input is Digit or UpperCase or LowerCase or Special Character using Switch Case.
How does this program work?
- In this program you are going to learn about how to check given Input is Digit or UpperCase or LowerCase or Special Character using PHP.
- This Problem is solved by using Switch Case then we can easily get the Output.
Here is the code
<html>
<head>
<title> PHP Program To Check if input is Digit or Alphabet or Special using Switch Case</title>
</head>
<body>
<form method="post">
<table border="0">
<tr>
<td><input type="text" name="input" value="" placeholder="Enter input"/></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Submit"/></td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit']))
{
$input_char = $_POST['input'];
echo "Given Input : ".$input_char; //To print given input
if (preg_match( '/\d/', $input_char ))
//There is at least one Digit inside the input
{
$ch = 1;
}
else
if (preg_match('/[A-Z]/', $input_char)) //There is at least one Upper Case Character inside the input
{
$ch = 2;
}
else
if(preg_match('/[a-z]/', $input_char)) //There is at least one Lower Case Character inside the input
{
$ch = 3;
}
else
if(preg_match('/[^a-zA-Z\d]/', $input_char)) //There is at least one Special Character inside the input
{
$ch = 4;
}
switch ($ch) //choose any case
{
case 1:
echo "Given Input is Number";
break;
case 2:
echo "Given Input is Upper Case";
break;
case 3:
echo "Given Input is Lower Case";
break;
case 4:
echo "Given Input is Special Character";
break;
default :
echo "Given input is wrong";
break;
}
return 0;
}
?>
</body>
</html>