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

后端 未结 2 1840
春和景丽
春和景丽 2021-01-18 23:53

I have these strings. I want a regular expression to match them and return true when I pass them to preg_match function.

do you want to eat katak at my home         


        
2条回答
  •  走了就别回头了
    2021-01-19 00:42

    $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

提交回复
热议问题