我正在使用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-";
$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"] . "
";
$element->href = $image["src"];
}
}
}在此先感谢您的帮助。