<?php
function separateAnchors($html) {
// Define a character sequence that
// will certainly not occur in your document,
// and is interpreted as literal in regular expressions:
$magicChar = "²³²";
$doc = new DOMDocument();
$doc->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXPath($doc);
$anchors = $xpath->query("//a");
$parent = $anchor->parentNode;
$origAnchor = $anchor->cloneNode(true);
// temporariy put the special text in the anchor
$anchor->textContent = $magicChar;
// and then take the document's text content
$txt = $doc->textContent;
// If that contains the special text with a non-space following it:
// ... then add a single space node after it, after
// any closing parent nodes
$elem = $anchor;
while (!$elem->nextSibling) $elem = $elem->parentNode;
$elem->parentNode->insertBefore($doc->createTextNode(" "),
$elem->nextSibling);
}
// Put original anchor back in place
$parent->replaceChild($origAnchor, $anchor);
}
return $doc->saveHTML();
}
// sample data
$html = "<p><a>first link</a> <a>second link</a>this word is too close</p>\n
<table><tr><td><a>table cell</a></td></tr></table><span>end</span>\n
<div><a>link</a></div><span><a>too close</a></span>";
// inject spaces
$html = separateAnchors($html);
// Show result
echo $html;
?>