![]() |
Here is simple Exampple to get Longest Word from a Sentence in 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"
?>
How to extract Longest Word from a Sentence in PHP | PHP String Tutorial |
Read Full: | PHP String Tutorial |
Category: | Web Tutorial |
Sub Category: | PHP String Tutorial |
Uploaded by: | Admin |
Views: | 351 |
Tested on: | PHP 7 |
Reffered: https://stackoverflow.com/questions/4599174/longest-word-in-a-string