这是一个DOMDocument
基于 - 的实现,它为您的 HTML 进行按书上的字符串替换:
$string = 'Hello :) <a title="Hello :)"> Bye :( </a>';
$items = array(
':)' => 'smile',
':(' => 'sad',
'=))' => 'laugh',
':p' => 'tongue',
);
foreach($items as $key => $class) $regex[] = preg_quote($key);
$regex = '#(?!<\w)('.implode('|', $regex).')(?!\w)#';
$doc = new DOMDocument();
$doc->loadHTML($string);
$xp = new DOMXPath($doc);
$text_nodes = $xp->query('//text()');
foreach ($text_nodes as $text_node)
{
$parent = $text_node->parentNode;
$context = $text_node->nextSibling;
$text = $text_node->nodeValue;
$matches = array();
$offset = 0;
$parent->removeChild($text_node);
while ( preg_match($regex, $text, $matches, PREG_OFFSET_CAPTURE, $offset) > 0 )
{
$match = $matches[0];
$smiley = $match[0];
$pos = $match[1];
$prefix = substr($text, $offset, $pos - $offset);
$offset = $pos + strlen($smiley);
$span = $doc->createElement('span', $smiley);
$span->setAttribute('class', $items[$smiley]);
$parent->insertBefore( $doc->createTextNode($prefix), $context );
$parent->insertBefore( $span, $context );
}
$suffix = substr($text, $offset);
$parent->insertBefore( $doc->createTextNode($suffix), $context );
}
$body = $doc->getElementsByTagName('body');
$html = $doc->saveHTML( $body[0] );
将它包装在一个函数中,你就可以开始了。它可能比正则表达式更多的代码行,但它不是一个丑陋的、充满错误的维护噩梦(就像任何基于正则表达式的解决方案一样)。