Reputation: 73
I need to create a regular expression to match only the first word of a sentence, when it is equal to or greater than 4 characters. I searched for truth and forums here and could not do it as ..
Example:
"Christmas Baskets"> "Christmas"
"Tea and infusions"> "Tea and infusions"
"Beer"> "Beer"
Upvotes: 0
Views: 117
Reputation: 173642
I'm not sure if I understood the question correctly, but this should give the output you've described:
function get_first_word_or_sentence($sentence)
{
$word = strtok($sentence, ' ');
return strlen($word) >= 4 ? $word : $sentence;
}
Upvotes: 2
Reputation: 2635
That's a strange use of RegEx. The following will only match a word at the beginning of a sentence that is longer than 3 characters:
/^[^\s][^\s][^\s][^\s]+/
Upvotes: 0