Mike Mike - 3 months ago 9
PHP Question

PHP regex to cut out everything between defined tags

I'm trying to get a regex that finds everything between those h4 tags. So i need an array that contains two paragraphs in the first element, one in the second, three in the third, and so on. There can be all kind of html between the h4 tags.

The one below is not working.

$a = preg_match_all("#<h4>(.*?)</p>.*?<h4>#s", $description, $m);





<h4>Lorem ipsum </h4>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</p>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</p>
<h4>Lorem ipsum </h4>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</p>
<h4>Lorem ipsum </h4>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</p>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</p>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</p>
<h4>Lorem ipsum </h4>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</p>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</p>

Lew Lew
Answer
$subject = "Your HTML"; //replace with actual HTML
$pattern = "/<\\/h4>(((?!<h4>).)*)/mis";
preg_match_all($pattern, $subject, $matches);

$matches is then an array of arrays containing all the text found between h4 tags globally.

Comments