Advertisement
Guest User

qs

a guest
Jun 23rd, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. <?php
  2. if($_SERVER['REQUEST_METHOD'] === 'POST'):
  3. // Receive the input
  4. $input = trim($_POST['input'], ', '); // remove any extra commas and spaces
  5. $arr1 = explode(', ', $input); // separated by comma and space
  6. $frq = array_count_values($arr1); // This function returns an array of keys and values. the keys are the original array values and the values are the frequency of elements
  7. arsort($frq); // arrange it from bigger to lower (most frequent to less)
  8.  
  9. // Get the array of the keys [The words]
  10. $frq = array_keys($frq);
  11.  
  12. // Now show the result
  13. $arranged = "";
  14. foreach($frq as $element){
  15. $arranged .= $element . " -> ";
  16. }
  17. $arranged = rtrim($arranged, '-> '); // remove the last "-> "
  18. echo "The words you entered from most frequent to less frequent are : " . $arranged;
  19. else:
  20. ?>
  21. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
  22. <input type="text" name="input" />
  23. <input type='submit' />
  24. </form>
  25. <?php endif; ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement