PHP-A8
PHP-A8
8. Write a PHP user interface program with an HTML form to input a string.
Upon submission, it will display the number of times each word occurs,
ignoring the distinction between capital and lowercase letters. It should also
print the most and least used words. Additionally, include buttons for sorting
data in ascending order and descending order.
<?php
session_start();
function analyzeString($input_string){
$input_string = strtolower($input_string);
// Split the input string into words and count their occurrences
$word_counts = array_count_values($words);
return $word_counts;
function displayWordFrequencies($word_counts){
if(!empty($word_counts)){
// Find the most and least used words based on the word counts
$most_used_word = '';
$least_used_word = '';
$max_count = 0;
$min_count = PHP_INT_MAX;
$most_used_word = $word;
$max_count = $count;
$least_used_word = $word;
$min_count = $count;
} else {
if(isset($_POST['analyze'])){
$input_string = $_POST['input_string'];
if(!empty($input_string)){
$word_counts = analyzeString($input_string);
$_SESSION['input_string'] = $input_string;
$_SESSION['word_counts'] = $word_counts;
displayWordFrequencies($word_counts);
} else {
if(isset($_POST['sort_asc']) || isset($_POST['sort_desc'])){
$word_counts = isset($_SESSION['word_counts']) ?
$_SESSION['word_counts'] : [];
if(isset($_POST['sort_asc'])){
asort($word_counts);
if(isset($_POST['sort_desc'])){
arsort($word_counts);
displayWordFrequencies($word_counts);
} else {
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method="post">
</form>
</body>
</html>
OUTPUT: