是的,不要使用正则表达式来解析HTML,而是使用DOM。
我会这样做:
在工具箱中,用于获取节点内部DOM的函数:
// GET INNER HTML OF A NODE
function DOMinnerHTML($element)
{
$innerHTML = "";
$children = $element->childNodes;
foreach ($children as $child)
{
$tmp_dom = new DOMDocument();
$tmp_dom->appendChild($tmp_dom->importNode($child, true));
$innerHTML.=trim($tmp_dom->saveHTML());
}
return $innerHTML;
}
然后是应用自定义函数的过滤器:
function change_destination_links_for_images( $content ) {
$dom = new DOMDocument();
// THIS IS HACK TO LOAD STRING WITH CORRECT ENCODING
// JUST OUTPUT <--?xml encoding="UTF-8"--> IN HTML SO NO HARM
$dom->loadHTML( \'<?xml encoding="UTF-8">\' . $content );
// GET ALL <a> NODE
foreach ( $dom->getElementsByTagName(\'a\') as $node ) {
// GET HREF
$link_href = $node->getAttribute( \'href\' );
// USE INNER OF THIS <a> NODE AS NEW DOC TO EXTRACT IMG
$dom_node = new DOMDocument();
$inner = DOMinnerHTML($node);
$dom_node->loadHTML($inner);
// CHECK IF IMAGE INSIDE THE LINK
if ( $dom_node->getElementsByTagName(\'img\') {
// IF SO REPLACE HREF
$node->setAttribute(\'href\', get_permalink() );
// RETURN MODIFIED DOM
$content = $dom->saveHTML();
}
}
// RETURN CONTENT
return $content;
}
// APPLY FILTER
add_filter( \'the_content\', \'change_destination_links_for_images\' );
这是我为另一个目的所做工作的快速调整,因此可能会有错误。希望这有帮助。