3v4l.org

run code in 300+ PHP versions simultaneously
<?php $HTML_string = ' <h1>Test of h1</h1> <p>This is a p test.</p> <h2>Test of h2</h2> <p>This is a p test with a <strong>strong emphasis</strong> followed by this.</p> <h3>Test of h3</h3> <p>This here is a link: <a href="http://www.example.com/1">Example.com</a>.<br> And this is a linebreak.</p> <p>Another paragraph, followed by a horizontal line:</p> <hr> <p>A final paragraph with <a href="http://www.example.com/2"><em>some emphasis</em> inside a link</a>.</p> '; function convertHTag(DOMNode $node) { $textNode = $node->ownerDocument->createTextNode(strtoupper($node->nodeName). ': ' . trim($node->textContent)); $node->ownerDocument->importNode($textNode); $node->parentNode->replaceChild($textNode, $node); } function convertATag(DOMNode $node) { $textNode = $node->ownerDocument->createTextNode(trim($node->textContent) . ' ( ' . $node->getAttribute('href') . ' ) '); $node->ownerDocument->importNode($textNode); $node->parentNode->replaceChild($textNode, $node); } function convertStrongTag(DOMNode $node) { $textNode = $node->ownerDocument->createTextNode(' **' . trim($node->textContent) . '** '); $node->ownerDocument->importNode($textNode); $node->parentNode->replaceChild($textNode, $node); } function convertEmTag(DOMNode $node) { $textNode = $node->ownerDocument->createTextNode(' *' . trim($node->textContent) . '* '); $node->ownerDocument->importNode($textNode); $node->parentNode->replaceChild($textNode, $node); } function convertPTag(DOMNode $node) { return trim($node->textContent); $node->ownerDocument->importNode($textNode); $node->parentNode->replaceChild($textNode, $node); } $doc = new DOMDocument(); $doc->loadHTML($HTML_string); $xpath = new DOMXpath($doc); foreach($xpath->query('/html/body/*[self::h1 or self::h2 or self::h3]') as $node) { convertHTag($node); } foreach($xpath->query('//a') as $node) { convertATag($node); } foreach($xpath->query('//strong') as $node) { convertStrongTag($node); } foreach($xpath->query('//em') as $node) { convertEmTag($node); } foreach($xpath->query('//p') as $node) { convertPTag($node); } echo $doc->textContent; /* H1: Test of h1 This is a p test. H2: Test of h2 This is a p test with a **strong emphasis** followed by this. H3: Test of h3 This here is a link: Example.com ( http://www.example.com/1 ). And this is a linebreak. Another paragraph, followed by a horizontal line: --------------- A final paragraph with *some emphasis* inside a link ( http://www.example.com/2 ). */
Output for 8.0.30, 8.1.22 - 8.1.28, 8.2.9 - 8.2.18, 8.3.0 - 8.3.6
H1: Test of h1 This is a p test. H2: Test of h2 This is a p test with a **strong emphasis** followed by this. H3: Test of h3 This here is a link: Example.com ( http://www.example.com/1 ) . And this is a linebreak. Another paragraph, followed by a horizontal line: A final paragraph with some emphasis inside a link ( http://www.example.com/2 ) .
Output for 7.1.25 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.29, 8.1.0 - 8.1.21, 8.2.0 - 8.2.8
H1: Test of h1 This is a p test. H2: Test of h2 This is a p test with a **strong emphasis** followed by this. H3: Test of h3 This here is a link: Example.com ( http://www.example.com/1 ) . And this is a linebreak. Another paragraph, followed by a horizontal line: A final paragraph with some emphasis inside a link ( http://www.example.com/2 ) .

preferences:
199.62 ms | 403 KiB | 181 Q