How to use preg_match to extract data?

前端 未结 5 1926
无人及你
无人及你 2021-01-07 04:21

I am pretty new to the use of preg_match. Searched a lot for an answer before posting this question. Found a lot of posts to get data based on youtube ID etc. But nothing as

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-07 04:59

    One solution is:

    \[#(\d+)\]
    

    This matches the left square bracket and pound sign [#, then captures one or more digits, then the closing right square bracket ].

    You would use it like:

    preg_match( '/\[#(\d+)\]/', '[#1234] Subject', $matches);
    echo $matches[1]; // 1234
    

    You can see it working in this demo.

提交回复
热议问题