PHP DOMDocument甚至跳过元素 [英] PHP DOMDocument skips even elements
问题描述
您好,我正在使用此方法将所有iframe和img标签替换为span标签
Hello I'm using this method to replace all iframe and img tags with span tags
$string = clean($string);
$dom = new \DOMDocument;
$dom->loadHTML(mb_convert_encoding($string, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$iframes = $dom->getElementsByTagName('iframe');
foreach($iframes as $iframe) {
$src = $iframe->getAttribute('src');
$span = $dom->createElement('span');
$span->setAttribute('title', $src);
$span->setAttribute('class', 'lazy-youtube');
$iframe->parentNode->replaceChild($span, $iframe);
}
$images = $dom->getElementsByTagName('img');
foreach($images as $image) {
$src = $image->getAttribute('src');
$span = $dom->createElement('span');
$span->setAttribute('title', $src);
$span->setAttribute('class', 'lazy-image');
$image->parentNode->replaceChild($span, $image);
}
$html = $dom->saveHTML();
return clean($html);
但问题在于它会跳过始终像这样的元素
but problem is that it skips elements it's always like this
// Iframe
<span>
<iframe>
<span>
<iframe>
<span>
<iframe>
<span>
<iframe>
// Img
<span>
<img>
<span>
<img>
<span>
<img>
<span>
<img>
iframe的HTML
Html for iframes
<div class="content">
<p>
<iframe frameborder="0" height="315" src="https://www.youtube.com/embed/T6kG5vuPVSs?rel=0" width="560"></iframe>
<p>
</p>
<iframe frameborder="0" height="315" src="https://www.youtube.com/embed/GjnadPBMJGs?rel=0" width="560"></iframe>
<p>
</p>
<iframe frameborder="0" height="315" src="https://www.youtube.com/embed/KYm8SLLQ0kk?rel=0" width="560"></iframe>
<p>
</p>
<iframe frameborder="0" height="315" src="https://www.youtube.com/embed/xUVz4nRmxn4?rel=0" width="560"></iframe>
<p>
</p>
<iframe frameborder="0" height="315" src="https://www.youtube.com/embed/hmZ6ziQJByY?rel=0" width="560"></iframe>
</p>
</div>
所有相同类型的元素具有相同的属性,只有src不同。谁知道我该如何修复它才能替换所有元素?
All same type of elements have same attributes, only src is different. Anyone know how can I fix it to replace all elements?
推荐答案
说明:问题是可能会跳过所有其他元素,因为例如,一旦删除iframe,对象(元素列表)就会发生变化,所有其他iframe都会移动以占据被删除的位置。
Explanation of the problem: It's probably skipping every other element because once you remove an iframe, for example, the object (the list of elements) changes in a way that all other iframes shift to ocuppy the removed's spot.
一个修复的方式:
// code
$iframes = $dom->getElementsByTagName('iframe');
while($iframes->length > 0){ // while there are still frames left to change
foreach($iframes as $iframe) {
// your regular code to replace iframe with span
// break; // this makes it easier to understand, but not really necessary
}
$iframes = $dom->getElementsByTagName('iframe'); // get the (remaining) skipped frames until there is none left
}
// code
别忘了对图像做同样的事情。
这是一个更好的方法了解问题所在:
Here is a better way to understand the problem:
1 - List of iframes
iframe1 iframe2 iframe3 iframe4 iframe5 [...]
/\ - current item in loop
2 - Replacing iframe1, it comes out of the list (since I just want iframes), so the list is now:
iframe2 iframe3 iframe4 iframe5 [...]
/\
3 - Loop continues and it goes to the next item
iframe2 iframe3 iframe4 iframe5 [...]
/\ - current item in loop
看看那样,它将跳过其他所有元素吗?
See how, that way, it would skip every other element?
这篇关于PHP DOMDocument甚至跳过元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!