Match all text between two HTML tags using a regex in PHP

夙愿已清 提交于 2019-12-12 06:13:34

问题


I have a problem with a regex pattern. It returns two arrays as a result... Here is my code:

$code = preg_match_all("/\< style\>(.*?)\<\/style\>/",$code,$matches);
var_dump($matches);

As test I set:

$code = ">< xxxxx> try blah fooo blah   < /xxxxx> idfidf oh < x>< /x> < style> blah blah blah style1 < /style>< style>blah blah style 2 x< /style>

It returns 2 arrays, I mean

$matches = array
    0 => array
        0 => string '< style> blah blah blah style1 < /style>' (length=38)
        1 => string '< style>blah blah style 2 x< /style>' (length=34)
    1 => array
        0 => string ' blah blah blah style1 ' (length=23)
        1 => string 'blah blah style 2 x' (length=19)

The matches I want are in the second array. I put space between the tags, because the editor not showing the HTML tags.


回答1:


Did you tried this:

echo $matches[0];
echo $matches[1]; // and so on, depend in the number of matches.



回答2:


Following code is working for me:

$code = "<xxxxx> try blah fooo blah </xxxxx> idfidf oh <x></x> <style> blah blah blah style1 </style><style>blah blah style 2 x</style>";
$code = preg_match_all("~<style>(.*?)</style>~si", $code, $matches);
var_dump($matches[1]);
  • Modifier s is for DOT_ALL (including newline)
  • Modifier i is for ignore case match

OUTPUT

array(2) {
  [0]=>
  string(23) " blah blah blah style1 "
  [1]=>
  string(19) "blah blah style 2 x"
}

However just to let you know that parsing HTML from regex is not a very good idea, you would be better off using many HTML parsers available for php.




回答3:


How about using Xpath? http://nl.php.net/manual/en/class.domxpath.php

Works for me (most of the times, that is ;) )



来源:https://stackoverflow.com/questions/6058986/match-all-text-between-two-html-tags-using-a-regex-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!