在标签与属性之间匹配文本的最佳方法

I am trying to get all text between two tags. There will be more than one tags pairs in the document, so I need to get all entries.

Here is an example of text

<pre class="code-class" id="code-error">
function error_notice() {
    ?>
    <div class="error notice">
        <p><?php _e( 'There has been an error', 'my_textdomain' ); ?></p>
    </div>
    <?php
}
add_action( 'admin_notices', 'my_error_notice' );
</pre>

As you can see there is nested code inside <pre> tag has HTML itself, but I need to capture all text between <pre> tags. Also I want to parse attributes class and id for example.

I tried to parse this text using DOMDocument

$dom = new DOMDocument;
$dom->loadHTML($htmlString);
$preTags = $dom->getElementsByTagName('pre');
foreach($preTags as $pre)
{

But nested HTML parsed as separate node.

I also tried using regex, but cannot parse all possible attributes.

Please suggest the best way to parse and consider all possible cases.

$input_lines= YOUR CODE;
preg_match_all("/<pre[^>]*>(.*?)<\/pre>/is", $input_lines, $output_array);
print_r($output_array);

demo http://www.phpliveregex.com/p/hSB

Use this to capture all content inside pre tag:

<pre.*?>(.*?)<\/pre>

capture group 1 contains the content that you are looking for

Try this regex over the entire string and get the classes and ids as for each match in capture group 1

class="(.*?)"|id="(.*?)"

Try here

Run the php sample here