Advertisement
dimipan80

Modify String

Apr 22nd, 2015
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.31 KB | None | 0 0
  1. <!--Write a PHP script StringModifier.php which receives a string from an input form and modifies it according to the selected option (radio button). You should support the following operations: palindrome check, reverse string, split to extract leters only, hash the string with the default PHP hashing algorithm, shuffle the string characters randomly. The result should be displayed right under the input field. Styling the page is optional. Think about which of the modification can be achieved with already built-in functions in PHP. Where necessary, write your own algorithms to modify the given string. Hint: Use the crypt() function for the "Hash String" modification.-->
  2.  
  3. <!DOCTYPE html>
  4. <html>
  5. <head lang="en">
  6.     <meta charset="UTF-8"/>
  7.     <title>String Modifier</title>
  8. </head>
  9. <body>
  10. <form method="post">
  11.     <p>
  12.         <input type="text" name="text" required/>
  13.         <input type="radio" name="modifier" id="palindrome" value="0" checked/>
  14.         <label for="palindrome">Check Palindrome</label>
  15.         <input type="radio" name="modifier" id="reverse" value="1"/>
  16.         <label for="reverse">Reverse String</label>
  17.         <input type="radio" name="modifier" id="split" value="2"/>
  18.         <label for="split">Split</label>
  19.         <input type="radio" name="modifier" id="hash" value="3"/>
  20.         <label for="hash">Hash String</label>
  21.         <input type="radio" name="modifier" id="shuffle" value="4"/>
  22.         <label for="shuffle">Shuffle String</label>
  23.         <input type="submit" value="Submit"/>
  24.     </p>
  25. </form>
  26. <p>
  27.     <?php
  28.     if (!array_key_exists('text', $_POST) || !array_key_exists('modifier', $_POST) ||
  29.         !isset($_POST['text']) || strlen(trim($_POST['text'])) === 0 || !isset($_POST['modifier'])
  30.     ) {
  31.         die('The Input Form is Empty or Data is Out of Range!!!');
  32.     }
  33.  
  34.     $text = trim($_POST['text']);
  35.     $modifier = (int)$_POST['modifier'];
  36.     $result = '';
  37.     switch ($modifier) {
  38.         case 1:
  39.             $result = strrev($text);
  40.             break;
  41.         case 2:
  42.             $arr = str_split($text);
  43.             $result = implode(" ", $arr);
  44.             break;
  45.         case 3:
  46.             $result = crypt($text, '$1$256$512$');
  47.             break;
  48.         case 4:
  49.             $result = str_shuffle($text);
  50.             break;
  51.         default:
  52.             if (strrev(strtolower($text)) == strtolower($text)) {
  53.                 $result = "$text is a palindrome!";
  54.             } else {
  55.                 $result = "$text is not a palindrome!";
  56.             }
  57.             break;
  58.     }
  59.  
  60.     echo htmlspecialchars($result);
  61.     ?>
  62. </p>
  63. </body>
  64. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement