Parsing Repeating Tags Using Regular Expressions in PHP
Suppose we have not one tag, but several. For example, several paragraphs:
<p>
text1
</p>
<p>
text2
</p>
<p class="block">
text3
</p>
Let's write code that will get an array of texts of all paragraphs:
<?php
preg_match_all('#<p[^>]*>(.+?)</p>#su', $str, $matches, PREG_PATTERN_ORDER);
?>
Let's check that we found the texts of our paragraphs:
<?php
var_dump($matches[1]); // ['text1', 'text2', 'text3']
?>
Get an array of contents of all h2:
<h2>111</h2>
<p>
---
</p>
<h2>222</h2>
<p>
---
</p>
<h2 class="last">333</h2>
<p>
---
</p>