In: Computer Science
Write a PHP script to display in frequency all the words in the
submitted paragraph.
Convert the string to an array of words.
Count the number of times the words are used. Hint:
array_count_values($arrayname)
Sort the array in frequency order.
Print the array to the screen. Hint: print_r($arrayname)
If you have any doubts, please give me comment...
<?php
if(isset($_POST['sentence'])){
$sentence = $_POST['sentence'];
//converting the string to an array of words
$words = explode(" ", $sentence);
// Count the number of times the words are used
$frequency = array_count_values($words);
// Sort the array in frequency order.
asort($frequency);
print_r($frequency);
}
else{
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="POST">
<p>
<div><b>Enter your sentence: </b></div>
<textarea name="sentence" row="5" cols="50"></textarea>
</p>
<button type="submit">Submit</button>
</form>
<?php
}
?>