Advertisement
dimipan80

HTML Tags Counter

Apr 14th, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.25 KB | None | 0 0
  1. <!--Write a PHP script HTMLTagsCounter.php which generates an HTML form like in the example below. It should contain a label, an input text field and a submit button. The user enters HTML tag in the input field. If the tag is valid, the script should print “Valid HTML tag!”, and if it is invalid – “Invalid HTML Tag!”. On the second line, there should be a score counter. For every valid tag entered, the score should increase by 1.
  2. Hint: You may use sessions. Use an array to store all valid HTML5 tags.-->
  3.  
  4. <!DOCTYPE html>
  5. <html>
  6. <head lang="en">
  7.     <meta content="text/html" charset="UTF-8">
  8.     <title>HTML Tags Counter</title>
  9. </head>
  10. <body>
  11. <form action="#" method="post">
  12.     <p><label for="htmlTag">Enter HTML tags:</label></p>
  13.  
  14.     <p><input type="text" name="htmlTag" id="htmlTag"/>
  15.         <input type="submit" value="Submit"/>
  16.     </p>
  17. </form>
  18. <?php
  19. session_start();
  20. if ($_POST && array_key_exists('htmlTag', $_POST)) {
  21.     $validHTML5Tags = array('!--...--', '!DOCTYPE', 'a', 'abbr', 'address', 'area', 'article', 'aside', 'audio',
  22.         'b', 'base', 'bdi', 'bdo', 'blockquote', 'body', 'br', 'button', 'canvas', 'caption', 'cite',
  23.         'code', 'col', 'colgroup', 'data', 'datalist', 'dd', 'del', 'details', 'dfn', 'dialog',
  24.         'div', 'dl', 'dt', 'em', 'embed', 'fieldset', 'figcaption', 'figure', 'footer', 'form',
  25.         'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'header', 'hgroup', 'hr', 'html', 'i', 'iframe',
  26.         'img', 'input', 'ins', 'kbd', 'keygen', 'label', 'legend', 'li', 'link', 'main', 'map',
  27.         'mark', 'menu', 'menuitem', 'meta', 'meter', 'nav', 'noscript', 'object', 'ol', 'optgroup',
  28.         'option', 'output', 'p', 'param', 'pre', 'progress', 'q', 'rb', 'rp', 'rt', 'rtc', 'ruby',
  29.         's', 'samp', 'script', 'section', 'select', 'small', 'source', 'span', 'strong', 'style',
  30.         'sub', 'summary', 'sup', 'table', 'tbody', 'td', 'template', 'textarea', 'tfoot', 'th',
  31.         'thead', 'time', 'title', 'tr', 'track', 'u', 'ul', 'var', 'video', 'wbr');
  32.  
  33.  
  34.     if (!array_key_exists('counter', $_SESSION)) {
  35.         $_SESSION['counter'] = 0;
  36.     }
  37.  
  38.     $checkTag = 'Invalid';
  39.     if (in_array($_POST['htmlTag'], $validHTML5Tags)) {
  40.         $checkTag = 'Valid';
  41.         $_SESSION['counter'] += 1;
  42.     }
  43.  
  44.     echo "<p>$checkTag HTML tag!</p>\n<p>Score: " . $_SESSION['counter'] . "</p>\n";
  45. }
  46. ?>
  47. </body>
  48. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement