# Return the Length of the longest word in sentence.

**URL:** <https://www.phphelp.com/t/return-the-length-of-the-longest-word-in-sentence/20613>\
**Category:** Code Snippets\
**Created:** [July 3, 2013, 6:38pm UTC](https://www.phphelp.com/t/return-the-length-of-the-longest-word-in-sentence/20613 "2013-07-03T18:38:44Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sensimillia](https://www.phphelp.com/letter_avatar_proxy/v4/letter/s/57b2e6/32.png) [@Sensimillia](https://www.phphelp.com/u/Sensimillia)\
**Post date:** [July 3, 2013, 6:38pm UTC](https://www.phphelp.com/t/return-the-length-of-the-longest-word-in-sentence/20613/1 "2013-07-03T18:38:44Z")

</div>

This functions can be used to prevent users from causing overflows with “words” of excessive length.

[php]  
function longestWord($sentence){  
$arr\_words = explode(" ",$sentence);  
foreach($arr\_words as $eWord){  
$eLen = strlen($eWord);  
if ($longest \< $eLen){  
$longest = $eLen;  
}  
}  
return $longest;  
}  
[/php]
