doushen2154 2014-06-09 20:53
浏览 37
已采纳

从内容中删除图像并将其放在第一个图像中

I want to move just the first <img> to the very first place in a <description> in an XML feed. I want to change this:

$html =
'this is content and this is the 1st image <img src="first_image.jpg"> within the text and <img src="second_image.jpg"> is the second one.'

to this:

$html = '<img src="the_source.jpg">
    this is content and this is the 1st image within the text and <img src="second_image.jpg"> is the second one.'

using DOMDocument in php. I have this right now:

$dom = new DOMDocument;
$dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
$xpath = new DOMXpath($dom);
if($image = $xpath->query('.//img[1]')){
    $image->parentNode->removeChild($image);
}
  • 写回答

1条回答 默认 最新

  • duankuaizhe8257 2014-06-09 20:59
    关注

    DOMXPath::query returns always a DOMNodeList object (even if there is a single node in the list), thus you need to take the first item (that is a DOMNode object) to use the parentNode property:

    if($image = $xpath->query('//img[1]')){
        $node = $image->item(0);
        $parent = $node->parentNode;
        $parent->removeChild($node);
        $parent->insertBefore($node, $parent->firstChild);
    }
    


    About your specific rss feed, it seems that the description tag is not the immediate ancestor of the img node. you can try this:
    $xml = file_get_contents('./rssfeed.xml');
    $xml = html_entity_decode($xml, ENT_XML1, "UTF-8");
    $xml = preg_replace('~<img\s[^>]*\K(?<!/)>~', '/>', $xml);
    
    $dom = new DOMDocument;
    $dom->loadXML($xml);
    
    $descNode = $dom->getElementsByTagName('description')->item(1);
    $imgNode = $descNode->getElementsByTagName('img')->item(0);
    
    $imgNode->parentNode->removeChild($imgNode);
    $descNode->insertBefore($imgNode, $descNode->firstChild);
    echo htmlspecialchars($dom->saveXML());
    

    If you want to preserve htmlentities:

    $dom = new DOMDocument;
    $dom->load('./rssfeed.xml');
    $descNode = $dom->getElementsByTagName('description')->item(1);
    $contentText = $descNode->nodeValue;
    $imgTag = '';
    $contentText = preg_replace_callback('~<img\s[^>]*>~',
                          function($m) use (&$imgTag) { $imgTag = $m[0]; return; },
                          $contentText, 1);
    $descNode->nodeValue = htmlentities($imgTag . $contentText);
    echo htmlspecialchars($dom->saveXML());
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数