简体   繁体   English

使用正则表达式从字符串中获取所有单词

[英]get all words from string with regular expression

I need preg_match that can find me all words from string. 我需要可以从字符串中找到所有单词的preg_match for example: 例如:

$str = "string: hi, it is string.";

I would like get this: 我想得到这个:

[0] => string
[1] => hi
[2] => it
[3] => is
[4] => string

I use with '/[az]+/ui' , but I get this: 我使用'/[az]+/ui' ,但是得到了:

[0] => string:
[1] => hi,
[2] => it
[3] => is
[4] => string.

You said preg_match() , instead you should be using preg_match_all() and there is no need to use the u modifier in your regular expression here. 您说的是preg_match() ,而是应该使用preg_match_all() ,这里不需要在正则表达式中使用u修饰符。

$str = "string: hi, it is string.";

preg_match_all('/[a-z]+/i', $str, $matches);
print_r($matches[0]);

Output 产量

Array
(
    [0] => string
    [1] => hi
    [2] => it
    [3] => is
    [4] => string
)

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

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