PHP Using RegEx to get substring of a string

前端 未结 4 472
自闭症患者
自闭症患者 2020-12-08 18:30

I\'m looking for an way to parse a substring using PHP, and have come across preg_match however I can\'t seem to work out the rule that I need.

I am parsing a web p

4条回答
  •  既然无缘
    2020-12-08 18:39

    Unfortunately, you have a malformed url query string, so a regex technique is most appropriate. See what I mean.

    There is no need for capture groups. Just match id= then forget those characters with \K, then isolate the following one or more digital characters.

    Code (Demo)

    $str = 'producturl.php?id=736375493?=tm';
    echo preg_match('~id=\K\d+~', $str, $out) ? $out[0] : 'no match';
    

    Output:

    736375493
    

提交回复
热议问题