我正在使用 simple_html_dom 管理 CMS 驱动的图像嵌入页面的方式。但是,我希望能够将尚未包装在锚标签中的任何图像包装在新的锚标签中,但我不知道如何用新标签包装 simple_html_dom 元素。
到目前为止我的代码:
$content = str_get_html($content);
if(is_object($content))
{
$elements = $content->find('img');
foreach($elements as $element)
{
/*
GET INFORMATION ABOUT WHAT IMAGE THIS IS - ALL FINE
*/
$classStr = $element->class;
if(trim(strlen($classStr)) > 0)
{
$needle = "wp-image-";
$after = substr($classStr, strpos($classStr, $needle) + strlen($needle));
if(strpos($after, " "))
{
$imageID = substr($after, 0, strpos($after, " "));
}
else
{
$imageID = $after;
}
/*
Get new image to link to
*/
$image = tona_get_image_by_id($imageID, "full");
/*
CHECK IF PARENT OF IMAGE IS AN ANCHOR... IF SO CHANGE THE LINK
*/
$elementParent = $element->parent();
if(isset($elementParent->href))
{
$elementParent->href = $image["src"];
$elementParent->class .= " newAnchorClass ";
}
/*
IF NOT ADD A NEW ANCHOR TAG ** HELP **
*/
echo "IMG: " . $imageID . " " . $image["src"] . "<br/>";
$element->href = $image["src"];
}
}
}
在此先感谢您的帮助。