我对正则表达式不好,但是我想用它从字符串中提取单词。

我需要的单词至少应包含4个字符,并且提供的字符串可以是utf8。

示例字符串:


苏珊·阿扎哈雷斯(sus azahares)提出了一些建议,请务必以书面形式告知对方(20至40岁)。


所需的输出:

Array(
    [0] => azahares
    [1] => presentan
    [2] => gruesos
    [3] => pétalos
    [4] => blancos
    [5] => teñidos
    [6] => rosa
    [7] => violáceo
    [8] => parte
    [9] => externa
    [10] => numerosos
    [11] => estambres
)

最佳答案

如果要查找的单词为UTF-8(根据规范,至少为4个字符),并且由ISO-8859-15的字母字符组成(对于西班牙语,但对于英语,德语,法语,等等。):

$n_words = preg_match_all('/([a-zA-Z]|\xC3[\x80-\x96\x98-\xB6\xB8-\xBF]|\xC5[\x92\x93\xA0\xA1\xB8\xBD\xBE]){4,}/', $str, $match_arr);
$word_arr = $match_arr[0];

关于php - 使用preg_match_all从字符串中提取单词,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10684183/

10-12 00:22
查看更多