Horje
How to get Longest Word from a Sentence in PHP

Here is simple Exampple to get Longest Word from a Sentence in PHP.


How to extract Longest Word from a Sentence in PHP

Follow the Example
index.php
Example: PHP
<?php
$string = "Where did the big Elephant go?";
$words = explode(' ', $string);
$longestWordLength = 0;
$longestWord = '';
foreach ($words as $word) { if (strlen($word) > $longestWordLength) { $longestWordLength = strlen($word); $longestWord = $word; }
}
echo $longestWord;
// Outputs: "Elephant"
?>





Category :
Web Tutorial
Sub Category :
PHP String Tutorial
Uploaded by :
Admin


Read Article
https://horje.com/learn/1434/reference