简体   繁体   中英

get all words from string with regular expression

I need preg_match that can find me all words from string. 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:

[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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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