Using PHP to check if each word in a string is a minimum number of characters?


Is there any simple way with PHP to verify that each word in a string is at least 5 characters?

I’ve looked into strlen and str_word_count but haven’t found anything that I can use to say "if each word is N length, do this". Each word is separated by a space.


Explode your string into an array, map each word to its length, call min() on the result.

function shortest_word_length($s)
    return min(array_map(function($word) { return strlen($word); }, explode(' ', $s)));
$s = 'this is a string';
echo shortest_word_length($s); // 1

Answered By – Siguza

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published