Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PHP preg_match multiple tags in html

I need to section off my html by multiple tags. from the start of the <h1> tag up to but not including the following <h1> tag. So far the expression I have works but only retrieves the first section. looking specifically for a preg_match solution. Would ideally like the solution to be dynamic (not matter the contents between the h1 tags or how many sections there are). Let me know (kinda new to regex in general). I know this may be a tricky question as I am new to php in general.

html

<h1>heading1</h1>
<img>
<p>para1</p>
<p>para2</p>
<h1>heading2</h1>
<img>
<p>para1</p>
<p>para2</p>
<h1>heading3</h1>
<img>
<p>para1</p>
<p>para2</p>

desired output or something similar:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

array{
  [0]=> <h1>heading1</h1>
        <img>
        <p>para1</p>
        <p>para2</p>

  [1]=> <h1>heading2</h1>
        <img>
        <p>para1</p>
        <p>para2</p>

  [2]=> <h1>heading3</h1>
        <img>
        <p>para1</p>
        <p>para2</p>
}

my current expression is:

$regex = '#<\/p>\s*(<h2>.*?)<h2>#s';
$preg = preg_match_all($regex, $content, $matches);
print_r ($matches);

Very thankful for any help!

>Solution :

preg_match_all() won’t return overlapping matches. Since your regexp ends with <h2>, which is the start of the next match, it won’t return that next match.

Put the start of the next match into a lookahead so it’s not included in the match.

$regex = '#<\/p>\s*(<h1>.*?)(?=<h1>)#s';
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading