Wednesday, September 2, 2015

Manual upper case function and lower case function and case_change function in php

<?php
//chr("ASCII value") will return a character
//ord('character') will return ASCII value of this respective character
//ord('a') = 97
//ord('z') = 123
//ord('A') = 65
//ord('Z') = 91
//chr("97") = a
//chr("123") = z
//chr("65") = A
//chr("91") = Z
function case_change($string){
   if(ord($string) < 97){
       $string = chr(ord($string) + 32);
   }else{
       $string = chr(ord($string) - 32);
   }
   return $string;
}

function upperCase($string){
    $new_string = "";
    $length = strlen($string);
    for($i = 0; $i < $length; $i++){
        if(ord($string[$i]) >= 97 && ord($string[$i]) <= 123){
            $new_string .= chr(ord($string[$i]) - 32);
        }else{
            $new_string .= $string[$i];
        }
    }
    return $new_string;
}

function lowerCase($string){
    $new_string = "";
    $length = strlen($string);
    for($i = 0; $i < $length; $i++){
        if(ord($string[$i]) >= 65 && ord($string[$i]) <= 91){
            $new_string .= chr(ord($string[$i]) + 32);
        }else{
            $new_string .= $string[$i];
        }
    }
    return $new_string;
}



if(isset($_POST['string'])){
    $string = $_POST['string'];
    $upperString = upperCase($string);
    $lowerString = lowerCase($string);
}



?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <h1>Put sentence in input box</h1>
    <form action="" method="post">
        <input type="text" name="string" id="" class="form-control"><br>
        <input type="submit" name="" id="" class="btn btn-lg btn-success">
    </form>
    <br>
    Upper Case: <br>
    <?php
    if(isset($upperString)){
        echo $upperString;
    }

    ?>
    <br>
    Lower Case: <br>
    <?php
    if(isset($lowerString)){
        echo $lowerString;
    }

    ?>
</div>
</body>
</html>

No comments:

Post a Comment

css snippet for blogger code highlighting

code, .code {     display: block;     background: beige;     padding: 10px;     margin: 8px 15px; }