I tried to use this to solve a problem: I would like to replace a text with a text link this way.
But if I use this source code:
$dom = new DOMDocument;
$dom->loadHTML($html_content);
function preg_replace_dom($regex, $replacement, DOMNode $dom, array $excludeParents = array()) {
if (!empty($dom->childNodes)) {
foreach ($dom->childNodes as $node) {
if ($node instanceof DOMText &&
!in_array($node->parentNode->nodeName, $excludeParents))
{
$node->nodeValue = preg_replace($regex, $replacement, $node->nodeValue);
}
else
{
preg_replace_dom($regex, $replacement, $node, $excludeParents);
}
}
}
}
preg_replace_dom('/match this text/i', "<a href='http://test.de'>test</a>", $dom->documentElement, array('a'));
The text gets replaced, but <a href='http://test.de>test</a>
is printed as plain text, not as link.
How can I solve this?