Extract words from string with preg_match_all

keyword, php, regex, string

Solution

This works if the words to look for are UTF-8 (at least 4 chars long, as per specs), consisting of alphabetic characters of ISO-8859-15 (which is fine for Spanish, but also for English, German, French, etc.):

$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];

Problem

I'm not good with regex but i want to use it to extract words from a string. The words i need should have minimum 4 characters and the provided string can be utf8. Example string: Sus azahares presentan gruesos pétalos blancos teñidos de rosa o violáceo en la parte externa, con numerosos estambres (20-40). Desired output: ``` 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 ) ```

Original source

Related problems