Horje
How to get Longest Word from a Sentence in PHP | PHP String Code
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"
?>



Read Full::
PHP String Code
Category:
PHP Tutorial Code
Sub Category:
PHP String Code
Uploaded:
1 month ago
Uploaded by:
Admin
Views:
137
Tested on:
PHP 7

Related Articles
How to extract words from Sentence in PHP PHP String Code
How to check if string is_numeric() Function in PHP PHP String Code
How to get only text from HTML Source Code by PHP PHP String Code
How to get first 3 words from a sentence by PHP PHP String Code
How to work if empty value does not else PHP String Code
How to get Longest Word from a Sentence in PHP PHP String Code
How to remove first character from string php PHP String Code
How to count if Number is below than 10 sum PHP String Code
How to get back page link PHP String Code

Single Articles
How to extract Longest Word from a Sentence in PHP

Share on: