Horje

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"
?>




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