简体   繁体   中英

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

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.

Did you tried this:

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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM