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 |
Type: | Php |
Category: | Web Tutorial |
Sub Category: | PHP String Tutorial |
Uploaded by: | Admin |