Why would this code:
$doc = new DOMDocument();
$doc->loadHTML($this->content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$imgNodes = $doc->getElementsByTagName('img');
if ($imgNodes->length > 0) {
$inlineImage = new Image();
$inlineImage->setPublicDir($publicDirPath);
foreach ($imgNodes as $imgNode) {
$inlineImage->setUri($imgNode->getAttribute('src'));
$inlineImage->setName(basename($inlineImage->getUri()));
if ($inlineImage->getUri() != $dstPath.$inlineImage->getName()) {
$inlineImage->move($dstPath);
$imgNode->setAttribute('src', $dstPath.'/'.$inlineImage->getName());
}
}
$this->content = $doc->saveHtml();
}
executed on this code:
<p><img alt="fluid cat" src="/images/tmp/fluid-cat.jpg"></p><p><img alt="pandas" src="/images/tmp/pandas.jpg"></p>
result in this code:
<p><img alt="fluid cat" src="/images/full/2016-09/fluid-cat.jpg"><p><img alt="pandas" src="/images/full/2016-09/pandas.jpg"></p></p>
Why does it place both img tags inside the first p block?
<div>....</div>for example, or removeLIBXML_HTML_NOIMPLIED) and extract its child nodes one by one to create the result string by concatenation./at the end of your img tag to make it self closingloadHTML()andsaveHTML()are terribly broken and useless in practice. Consider using a third-party HTML parser like html5lib-php and a custom HTML-code generator.