Advertisement
dimipan80

Most Frequent Tag

Dec 2nd, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.10 KB | None | 0 0
  1. <!--Write a PHP script MostFrequentTag.php which generates an HTML input text field and a submit button.
  2. In the text field the user must enter different tags, separated by a comma and a space (", ").
  3. When the information is submitted, the script should generate a list of the tags, sorted by frequency.
  4. Then you must print: "Most Frequent Tag is: [most frequent tag]". Semantic HTML is required.
  5. Styling is not required.-->
  6.  
  7. <!DOCTYPE html>
  8. <html>
  9. <head lang="en">
  10.     <meta content="text/html" charset="UTF-8">
  11.     <title>Most Frequent Tag</title>
  12. </head>
  13. <body>
  14. <form action="#" method="get">
  15.     <p><label for="tags">Enter Tags:</label></p>
  16.     <input type="text" name="tags" id="tags" required/>
  17.     <input type="submit" value="Submit"/>
  18. </form>
  19. <?php
  20. if ($_GET && $_GET['tags']) {
  21.     $tags = explode(', ', trim($_GET['tags']));
  22.     $tags = array_count_values($tags);
  23.     arsort($tags);
  24.  
  25.     foreach ($tags as $key => $value) {
  26.         echo '<p>' . htmlspecialchars($key) . " : $value times</p>\n";
  27.     }
  28.  
  29.     $keys = array_keys($tags);
  30.     echo '<p>Most Frequent Tag is: ' . htmlspecialchars($keys[0]) . "</p>\n";
  31. }
  32. ?>
  33. </body>
  34. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement