PHP- Regex to match words with more than two letters

I'm trying to explode a string into a word array, the condition is that a word is considered as such only if it has two or more letters, what I have is the following, but words with a single letter are still being considered as match:

$input_string = "I have a cake inside my fridge";

$string_array = preg_split("/[^\w{2,}]*([\s]+([^\w{2,}])*|$)/", $input_string, -1, PREG_SPLIT_NO_EMPTY);

But I'm still getting the words "I" and "a", why it isn't working?

Upvotes: 1

Views: 789

Answers (2)

The fourth bird
The fourth bird

Reputation: 163632

The reason it is not working is because the pattern [^\w{2,}]*([\s]+([^\w{2,}])*|$) matches only spaces, and then you split on those spaces resulting in an array with all the words. This is due to \s which matches a whitespace char, and using the negated character class [^\w{2,}] which also matches whitespace chars.

If you want to use split, you also have to match the single word characters so that they are not part of the result.


If you must use split, you can match either a single word character surrounded by optional horizontal whitespace characters to remove those as well, or match 1+ horizontal whitespace characters.

\h*\b\w\b\h*|\h+

Regex demo

For example

$input_string = "I have a cake inside my fridge";
$string_array = preg_split("/\h*\b\w\b\h*|\h+/", $input_string, -1, PREG_SPLIT_NO_EMPTY);
print_r($string_array);

Output

Array
(
    [0] => have
    [1] => cake
    [2] => inside
    [3] => my
    [4] => fridge
)

If you want to match all strings that consist of at least 2 characters, you could also use \S{2,} with preg_match_all.

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522762

If you just want to capture all "words" having 2 or more letters, then just use preg_match_all here:

$input_string = "I have a cake inside my fridge";
preg_match_all("/\b\w{2,}\b/", $input_string, $matches);
print_r($matches[0]);

This prints:

Array
(
    [0] => have
    [1] => cake
    [2] => inside
    [3] => my
    [4] => fridge
)

Upvotes: 1

Related Questions