mgibson Blog Banner

mgibson

How to find the largest and smallest word in a sentence using php script?

I have written a code in php that can help you find the largest and smallest word in a sentence.

if(isset($_POST['submit'])) { if(isset($_POST['txt'])) {

$s = $_POST['txt']; } if(isset($_POST['message'])) {

$p = $_POST['message']; } }

$s = explode(" ", $s); $p = explode(" ", $p);

if(!empty($s)){

$length = array_map("strlen",$s); $maxlength = max($length); $keyforlarge = array_search($maxlength, $length); $minlength = min($length); $keyforsmall = array_search($minlength, $length);

echo "Largest word in $s: " . $s[$keyforlarge] . " | " . " Length: " . $maxlength; echo "
"; echo "Smallest word in $s: " . $s[$keyforsmall] . " | " . " Length: " . $minlength; echo "

";

}

if(!empty($p)){

$wordlength = array_map("strlen",$p); $maxwordlength = max($wordlength); $keyforlargeword = array_search($maxwordlength, $wordlength); $minwordlength = min($wordlength); $keyforsmallword = array_search($minwordlength, $wordlength);

echo "Largest word in $p: " . $p[$keyforlargeword] . " | " . " Length: " . $maxwordlength; echo "/
"; echo "Smallest word in $p: " . $p[$keyforsmallword] . " | " . " Length: " . $minwordlength;

}

?>

This code is helpful to find the largest and smallest word in a sentence and a paragraph.

First you create a html file with a textbox and a textarea including a button. Then save the file in the name of index.php. Second file will have php code to find the largest and smallest word and save it with any name and mention the php file name in action attribute within form tag in index.php file.

When you press submit button after writing a sentence in textbox and a paragraph in textarea, it will redirect to php script and give the output with the largest and smallest word in the sentence and the paragraph. Both the file must be saved in one folder.

Follow me


Return from to mgibson's Web3 Blog

How to find the largest and smallest word in a sentence using php script? was published on and last updated on 19 Feb 2018.