<?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 git.master, git.master_jit, rfc.property-hooks
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 ) .
This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.