Home > Net >  Check if char is the same as alphabet letter condition regardless of uppercase and lowercase | PHP &
Check if char is the same as alphabet letter condition regardless of uppercase and lowercase | PHP &

Time:10-25

Here's the sample code:
What I Want To Achieve: Regardless of it being uppercase and lowercase, if the user inputs a letter that's the same as the condition whether it's also uppercase and lowercase, I want it to be true. A = a, b = B.

Ex: (D or d == d) == true | Output: 3 - DEF (A or c == a or c) == true | Output: 2 - ABC

<!DOCTYPE html>
<html lang = "en">
<head>
  <title>Test</title>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- BOOTSTRAP 5 -->
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <!-- jQuery -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
    </script>
    <!-- Sweet Alert 2 -->
    <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js">
    </script>
</head>

<body>

   <!-- Input and Output -->
            <!-- INPUT -->
            <div class="card">
                <div class="card-header"> Input Letter: </div>
                <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
                    <div class="card-body">
                        <div class="row">
                            <div class="col-12">
                                <input type="text" name="telephoneCharacter" placeholder="Input letter." class="form-label input-responsive" maxlength = "1">
                            </div>
                            <div class="col-12">
                                <div class="col-auto d-flex justify-content-sm-end">
                                    <input type="submit" class="btn btn-dark">
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>

            <!-- OUTPUT -->
            <div class="card-img-bottom d-flex justify-content-center">
                <div class="text-uppercase">
                    <p class="Display-1"> Output: </p>
                    <div class="container-fluid">
                        <?php
                        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
                            $character = $_POST['telephoneCharacter'];

                            if ( ($character == 'A' || $character == 'a' ) || ($character == 'B' || $character == 'b' ) || ($character == 'C' || $character == 'c') ) {
                                print "<p class='text-muted h4'> 2 - ABC</p>";
                            } else if ( ($character == 'D' || $character == 'd' ) || ($character == 'E' || $character == 'e' ) || ($character == 'F' || $character == 'f') ) {
                                print "<p class='text-muted h4'> 3 - DEF</p>";
                            }  else if ( ($character == 'G' || $character == 'g' ) || ($character == 'H' || $character == 'h' ) || ($character == 'I' || $character == 'i') ) {
                                print "<p class='text-muted h4'> 4 - GHI</p>";
                            } else if ( ($character == 'J' || $character == 'j' ) || ($character == 'K' || $character == 'k' ) || ($character == 'L' || $character == 'l') ) {
                                print "<p class='text-muted h4'> 5 - JKL</p>";
                            } else if ( ($character == 'M' || $character == 'm' ) || ($character == 'N' || $character == 'n' ) || ($character == 'O' || $character == 'o') ) {
                                print "<p class='text-muted h4'> 6 - MNO</p>";
                            }                                         

                        }
                        ?>
                    </div>
                </div>
            </div>
            
            <!-- BOOTSTRAP 5 Script -->
                <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP JcXn/tWtIaxVXM" crossorigin="anonymous"></script>

</body>
</html>

CodePudding user response:

Create an array of valid characters all in the same case (upper in this case) and then compare the input, converted to upper with entries in the array

$character = 'd';

if ( in_array(strtoupper($character), ['A','B','C'] )) {
    print "<p class='text-muted h4'> 2 - ABC</p>";
}

if ( in_array(strtoupper($character), ['D','E','F'] )) {
    print "<p class='text-muted h4'> 3 - DEF</p>";
}
if ( in_array(strtoupper($character), ['G','H','I'] )) {
    print "<p class='text-muted h4'> 4 - GHI</p>";
}
if ( in_array(strtoupper($character), ['J','K','L'] )) {
    print "<p class='text-muted h4'> 5 - JKL</p>";
}
if ( in_array(strtoupper($character), ['M','N','O'] )) {
    print "<p class='text-muted h4'> 6 - MNO</p>";
}                                         

  • Related