regex php: find everything in div

前端 未结 3 1521
迷失自我
迷失自我 2020-12-06 22:07

I\'m trying to find eveything inside a div using regexp. I\'m aware that there probably is a smarter way to do this - but I\'ve chosen regexp.

so currently my regexp

3条回答
  •  粉色の甜心
    2020-12-06 22:42

    What about something like this :

    $str = <<text to extract here
    HTML; $matches = array(); preg_match_all('#]*>(.*?)
#', $str, $matches); var_dump($matches[1]);

Note the '?' in the regex, so it is "not greedy".

Which will get you :

array
  0 => string 'text to extract here' (length=20)
  1 => string 'text to extract from here as well' (length=33)

This should work fine... If you don't have imbricated divs ; if you do... Well... actually : are you really sure you want to use rational expressions to parse HTML, which is quite not that rational itself ?

提交回复
热议问题