Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if($_SERVER['REQUEST_METHOD'] === 'POST'):
- // Receive the input
- $input = trim($_POST['input'], ', '); // remove any extra commas and spaces
- $arr1 = explode(', ', $input); // separated by comma and space
- $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
- arsort($frq); // arrange it from bigger to lower (most frequent to less)
- // Get the array of the keys [The words]
- $frq = array_keys($frq);
- // Now show the result
- $arranged = "";
- foreach($frq as $element){
- $arranged .= $element . " -> ";
- }
- $arranged = rtrim($arranged, '-> '); // remove the last "-> "
- echo "The words you entered from most frequent to less frequent are : " . $arranged;
- else:
- ?>
- <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
- <input type="text" name="input" />
- <input type='submit' />
- </form>
- <?php endif; ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement