0% found this document useful (0 votes)
2 views

PHP-A8

The document provides a PHP program that creates a user interface for analyzing word frequencies in a given string. It includes functionality to count occurrences of each word, identify the most and least used words, and sort the results in ascending or descending order. The program utilizes HTML forms for user input and session management to retain data across submissions.

Uploaded by

agastua8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

PHP-A8

The document provides a PHP program that creates a user interface for analyzing word frequencies in a given string. It includes functionality to count occurrences of each word, identify the most and least used words, and sort the results in ascending or descending order. The program utilizes HTML forms for user input and session management to retain data across submissions.

Uploaded by

agastua8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

PART-A III BCA

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 to analyze the input string and calculate word frequencies

function analyzeString($input_string){

$input_string = strtolower($input_string);

// Split the input string into words and count their occurrences

$words = str_word_count($input_string, 1);

// Count the occurrences of each word

$word_counts = array_count_values($words);

return $word_counts;

// Function to display word frequencies

function displayWordFrequencies($word_counts){

echo "<h3>Word Frequencies:</h3>";

if(!empty($word_counts)){

foreach($word_counts as $word => $count){

echo "<p><strong>$word:</strong> $count times</p>";

PHP and MySQL Lab Page 29


PART-A III BCA

// 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;

foreach($word_counts as $word => $count){

if($count > $max_count || ($count == $max_count && strlen($word) <


strlen($most_used_word))){

$most_used_word = $word;

$max_count = $count;

if($count < $min_count || ($count == $min_count && strlen($word) <


strlen($least_used_word))){

$least_used_word = $word;

$min_count = $count;

echo "<p><strong>The most used word is:</strong> $most_used_word (used


$max_count times)</p>";

echo "<p><strong>The least used word is:</strong> $least_used_word (used


$min_count times)</p>";

} else {

echo "<p>No words found in the input string</p>";

PHP and MySQL Lab Page 30


PART-A III BCA

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 {

echo "<p>Please enter a string to analyze and sort</p>";

if(isset($_POST['sort_asc']) || isset($_POST['sort_desc'])){

$input_string = isset($_SESSION['input_string']) ? $_SESSION['input_string'] :


'';

$word_counts = isset($_SESSION['word_counts']) ?
$_SESSION['word_counts'] : [];

if(!empty($input_string) && !empty($word_counts)){

if(isset($_POST['sort_asc'])){

asort($word_counts);

PHP and MySQL Lab Page 31


PART-A III BCA

if(isset($_POST['sort_desc'])){

arsort($word_counts);

displayWordFrequencies($word_counts);

} else {

echo "<p>Please enter a string to analyze and sort</p>";

?>

<!DOCTYPE html>

<html>

<head>

<title>Word Frequency Analyzer</title>

</head>

<body>

<h2>Word Frequency Analyzer</h2>

<form method="post">

<label for="input_string">Enter a string:</label><br>

<input type="text" id="input_string" name="input_string" value="<?php echo


isset($_SESSION['input_string']) ? $_SESSION['input_string'] : ''; ?>"><br><br>

<input type="submit" name="analyze" value="Analyze">

PHP and MySQL Lab Page 32


PART-A III BCA

<input type="submit" name="sort_asc" value="Sort Ascending">

<input type="submit" name="sort_desc" value="Sort Descending">

</form>

</body>

</html>

PHP and MySQL Lab Page 33


PART-A III BCA

OUTPUT:

PHP and MySQL Lab Page 34


PART-A III BCA

PHP and MySQL Lab Page 35

You might also like