Php search string (with wildcards)

后端 未结 5 1003
自闭症患者
自闭症患者 2021-01-05 03:27

Is there a way to put a wildcard in a string? The reason why I am asking is because currently I have a function to search for a substring between two substrings (i.e grab th

5条回答
  •  梦谈多话
    2021-01-05 03:58

    Use a regex.

    $string = "My dog has fleas";
    if (preg_match("/\S+ (\S+) has fleas/", $string, $matches))
      echo ($matches[1]);
    else
      echo ("Not found");
    

    \S means any non-space character, + means one or more of the previous thing, so \S+ means match one or more non-space characters. (…) means capture the content of the submatch and put into the $matches array.

提交回复
热议问题