preg_match 或 preg_match_all 查找字符串中的所有单词以及它们之间的任何内容

preg_match or preg_match_all to find all words in string with anything between them

我想查找字符串中的所有单词。 用户可以以任何模式添加单词,如逗号分隔或;分开或任何东西。

输入:你好,你好,测试词

输出: array("Hello","Hi","test","word");

如果一个单词是只包含单词字符的字符串\w,你可以这样做:

preg_match_all('/(\w+)/', $input_string, $matches);

或使用preg_split:

$words = preg_split('/\W+/', $input_string);