3v4l.org

run code in 500+ PHP versions simultaneously
<?php $html = <<<'__HTML' <html xmlns:v="urn:schemas-microsoft-com:vml"... other xmlns> <head> <meta tags, title, styles, a couple comments too (they are irrelevant to the question)> </head> <body lang=EN-US link=blue vlink=purple style='tab-interval:36.0pt'> <div class=WordSection1> <h1>Pros and Cons of a Website</h1> <p class=MsoBodyText align=left style='a long irrelevant list'><span style='long list'><o:p>&nbsp;</o:p></span></p>(this is a sample of what it uses as line breaks. Take note of the <o:p> tag). <p class=MsoBodyText style='margin-right:5.75pt;line-height:115%'> A<span style='letter-spacing:.05pt'> </span>SAMPLE<span style='letter-spacing:.05pt'> </span>TEXT </p> </div> <div class=WordSection2>...same pattern in div 1</div> <div class=WordSection3>...same...</div> </body> </html><html xmlns:v="urn:schemas-microsoft-com:vml"... other xmlns> <head> <meta tags, title, styles, a couple comments too (they are irrelevant to the question)> </head> <body lang=EN-US link=blue vlink=purple style='tab-interval:36.0pt'> <div class=WordSection1> <h1>Pros and Cons of a Website</h1> <p class=MsoBodyText align=left style='a long irrelevant list'><span style='long list'><o:p>&nbsp;</o:p></span></p>(this is a sample of what it uses as line breaks. Take note of the <o:p> tag). <p class=MsoBodyText style='margin-right:5.75pt;line-height:115%'> A<span style='letter-spacing:.05pt'> </span>SAMPLE<span style='letter-spacing:.05pt'> </span>TEXT </p> </div> <div class=WordSection2>...same pattern in div 1</div> <div class=WordSection3>...same...</div> </body> </html> __HTML; libxml_use_internal_errors(true); $doc = new DomDocument(); $doc->loadHTML($html); foreach ($doc->getElementsByTagName('div') as $div) { $raw = $div->textContent; $text = trim(preg_replace('/\s+/', ' ', $raw)); var_dump($text); }
Output for 8.1.32 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.6
string(115) "Pros and Cons of a Website  (this is a sample of what it uses as line breaks. Take note of the tag). A SAMPLE TEXT" string(24) "...same pattern in div 1" string(10) "...same..." string(115) "Pros and Cons of a Website  (this is a sample of what it uses as line breaks. Take note of the tag). A SAMPLE TEXT" string(24) "...same pattern in div 1" string(10) "...same..."

preferences:
43.04 ms | 879 KiB | 4 Q