Advertisement
dimipan80

Coloring Texts

Apr 23rd, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.12 KB | None | 0 0
  1. <!--Write a PHP program TextColorer.php that takes a text from a textfield, colors each character
  2. according to its ASCII value and prints the result. If the ASCII value of a character is odd,
  3. the character should be printed in blue. If it’s even, it should be printed in red.-->
  4.  
  5. <!DOCTYPE html>
  6. <html>
  7. <head lang="en">
  8.     <meta charset="UTF-8">
  9.     <title>Coloring Texts</title>
  10.     <style type="text/css">
  11.         body > p {
  12.             font-family: "Courier New", monospace;
  13.             font-size: 18px;
  14.             color: #F00;
  15.         }
  16.  
  17.         span {
  18.             color: #00F;
  19.         }
  20.     </style>
  21. </head>
  22. <body>
  23. <form method="post">
  24.     <textarea name="text" cols="50" rows="5" placeholder="Enter your text..."></textarea>
  25.  
  26.     <p><input type="submit" value="Color text"/></p>
  27. </form>
  28. <p>
  29.     <?php
  30.     mb_internal_encoding('UTF-8');
  31.     if (!isset($_POST) || !isset($_POST['text']) || trim($_POST['text']) === '') {
  32.         die("The Input Form can't been Empty!!!");
  33.     }
  34.  
  35.     $textArr = str_split(trim($_POST['text']));
  36.     foreach ($textArr as $char) {
  37.         if (ord($char) % 2 !== 0) {
  38.             echo '<span>' . $char . ' </span>';
  39.         } else {
  40.             echo $char . ' ';
  41.         }
  42.     }
  43.     ?>
  44. </p>
  45. </body>
  46. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement