简体   繁体   English

PHP-正则表达式匹配超过两个字母的单词

[英]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?但是我仍然得到“I”和“a”这两个词,为什么它不起作用?

If you just want to capture all "words" having 2 or more letters, then just use preg_match_all here:如果您只想捕获具有 2 个或更多字母的所有“单词”,则只需在此处使用preg_match_all

$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
)

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.它不起作用的原因是因为模式[^\\w{2,}]*([\\s]+([^\\w{2,}])*|$)只匹配空格,然后你拆分这些空格产生一个包含所有单词的数组。 This is due to \\s which matches a whitespace char, and using the negated character class [^\\w{2,}] which also matches whitespace chars.这是由于\\s匹配空白字符,并使用否定字符类[^\\w{2,}]也匹配空白字符。

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.如果您必须使用拆分,您可以匹配由可选水平空白字符包围的单个单词字符以删除它们,或者匹配 1+ 个水平空白字符。

\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 .如果要匹配至少包含 2 个字符的所有字符串,还可以将\\S{2,}preg_match_all 一起使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM