One way you can do it, although it's not very elegant:
$content = 'Testing a test with this! Tag: <span title="test">tag</span>. <h1>Test heading</h1>';
//replace keywords (that are not part of HTML tags)
$content = preg_replace('~\btest\b(?![^<]*?>)~i', '<a href="" UNIQUE>$0</a>', $content);
//remove created links between heading tags
function _callback($matches) {
return preg_replace('~<a href="[^"]*" UNIQUE>(.*?)</a>~s', '$1', $matches[0]);
}
$content = preg_replace_callback('~<h([1-6])\b[^>]*>.+?</h\1>~is', '_callback', $content);
//remove UNIQUE marks
$content = preg_replace('~(<a href="[^"]*") UNIQUE>~', '$1>', $content);
header('Content-type: text/plain; charset=utf-8');
echo $content;