How to match any word in a String with Regex in PHP

烈酒焚心 提交于 2019-12-01 20:07:45

Use a quantifier:

$pattern = "/do you want to eat ([a-z0-9]*) at my hometown\?/i";
//                                here __^

and escape the ? ==> \?

revo
$text = "do you want to eat meatball at my hometown?";
$pattern = "/(\w+)(?=\sat)/";
if (preg_match($pattern, $text))

(\w+) matches one or more word characters.

(?=\sat) is a positive lookahead that matches one whitespace \s and the letters at.

Regex live demo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!