In this article I would like to share with you a sample program that I wrote using PHP that will ask the user to give a sentence and then our program will check and determine which of the word in the given sentence is the longest. The code is very simple and easy to understand.
I am currently accepting programming and web development work kindly contact me in the following email address for further details. Thank you.
My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.
My telephone number at home here in Bacolod City, Negros Occidental is (034) 4335675.
Sample Program Output
Program Listing
long.php
<!-- long.php -->
<!-- Written By Mr. Jake R. Pomperada, MAED-IT -->
<!-- February 2, 2018 Friday 4:03 PM -->
<!-- jakerpomperada@yahoo.com and jakerpomperada@gmail.com -->
<!-- Bacolod City, Negros Occidental Philippines -->
<!DOCTYPE html>
<html>
<head>
<title>Longest Word in PHP</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<style>
.art {
font-family: arial;
font-weight: bold;
color:red;
text-align: center;
width:45%;
background-color:yellow;
}
.art2 {
font-family: arial;
font-weight: bold;
font-size: 25px;
color:blue;
text-align: center;
width:45%;
background-color:lightgreen;
}
body {
font-family: arial;
font-weight: bold;
font-size: 20px;
}
input[type=submit] {
background-color: #008CBA;
font-family: arial;
font-weight: bold;
font-size: 20px;
height: 50px;
width: 120px
text-align: center;
cursor: pointer;
}
input[type=text] {
font-family: arial;
font-weight: bold;
font-size: 20px;
}
</style>
<body>
<?php
error_reporting(0);
?>
<h1 class="art"> Longest Word in PHP </h1>
<p class="art2"> Created By Mr. Jake R. Pomperada, MAED-IT </p>
<br><br>
<form method="post">
Enter a Sentence :
<input type="text" name="val_str" size="50" autofocus=""><br><br>
<input type="submit" name="submit" value="Check">
<input type="submit" name="clear" value="Clear">
</form><br><br><br>
<?php
function longest_word($str_test)
{
$words = explode(' ', $str_test);
$longestWordLength = 0;
$longestWord = '';
foreach ($words as $word) {
if (strlen($word) > $longestWordLength) {
$longestWordLength = strlen($word);
$longestWord = $word;
}
}
return strtoupper($longestWord);
}
if(isset($_POST['submit']))
{
$str_value = $_POST['val_str'];
$display = "The longest word is " .longest_word($str_value).".";
echo "The sentence : " .$str_value;
echo "<br><br>";
echo $display;
}
if(isset($_POST['clear']))
{
$display="";
}
?>
</body>
</html>
No comments:
Post a Comment