The following code splits a string and selects all of the alphanumeric words containing minimum 3 letters. However instead of getting all the words from the string, I want to get only first 3 valid words, but the problem is with the loop. How can I stop the loop as soon as 3 valid words are found from the string.
PS. If there is an alternative approach to the same thing, please suggest.
Thanks.
$string = "test test TEST test- -test _test blsdk.bldf,las";
$arr = preg_split('/[,\ \.;]/', $string);
$keywords = array_unique($arr);
foreach ($keywords as $keyword){
if ((preg_match("/^[a-z0-9]/", $keyword) ) && (strlen($keyword) > 3)){
echo $keyword;
echo "<br />";
}
}
add a variable which counts up when a matching keyword is found. when counter reaches maximum then break the
foreachloop