简体   繁体   中英

php regex match any number of words

I am new to php regex. I am trying to match any number of words. For eg: A 'person name' can have any number of words, how can I do this?

I tried using \\w+\\ , but its matching only 1 word.

DEMO

You may try this,

preg_match('~^\w+(?:\h+\w+)*$~', $str);

DEMO

You can use the following for matching any number of words separated by whitespace:

[\w\s]+

See DEMO

PHP:

preg_match('~^[\w\s]+$~', $str);

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