Preg_Match fetching all lines after a search word

时间:2019-01-15 18:11:29

标签: php regex preg-match

I have the following html response:

   Your Balances: <br><br>Points Balance: 600.03<br>
   <br>Last Transaction(s)
   <br>01/11/2019 100050000000 Location1 $14.00
   <br>11/28/2018 100053700000 Location2 $10.50
   <br>10/03/2018 100051800000 Location3 $20.00
   <br>06/26/2018 100047400000 Location4 $17.50
   <br>06/04/2018 100046400000 Location5 $7.00
   <br>

I need to use preg_match to extract the all the text below the Last Transaction(s) part. What I have tried is the following but cannot get all the text:

preg_match('/Last.*Transaction\(s\)<br>((.)+)/',$input, $output);

but I only get a single line 01/11/2019 100050000000 Location1 $14.00 back.

Can anyone please assist in getting all the text as I specified above?

1 个答案:

答案 0 :(得分:1)

您可以使用\G标志来做到这一点,

(?:                             # non-capturing group
    \G(?!\A)                    # match after the last match
    |                           # or
    \QLast Transaction(s)\E\s+  # Last Transactions(s) lit. + whitespaces
)
\s*<br>\K                       # whitespaces + <br>, 
                                # "forget what's been matched thus far (\K)"
(?P<value>.+)                   # capture anything in that line

请参见a demo on regex101.com(请注意修饰符!),但是请注意,解析HTML(通常是嵌套结构)被认为是不好的做法。如有可能,请使用解析器。