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

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);
}

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());