我必须创建一个以下html结构:
<figure class="op-interactive">
<iframe>
<div>
<script async="" height="" src="src" width=""></script>
</div>
</iframe>
</figure>
这是我的代码:
$media_embed = $doc->getElementsByTagName('div');
foreach ($media_embed as $media){
$scriptstag = $media->getElementsByTagName('script');
foreach ($scriptstag as $scripts) {
$src= $scripts->getAttribute('src');
$newFigure = $doc->createElement('figure');
$newFigure->setAttribute('class','op-interactive');
$newiFrame = $doc->createElement('iframe');
$newFigure->appendChild($newiFrame);
$newDiv = $doc->createElement('div');
$newiFrame->appendChild($newDiv);
$scripts->parentNode->insertBefore($newFigure, $scripts);
$newiFrame->appendChild($scripts);
}
}
$body1 = $doc->saveHTML($doc->documentElement);
但是输出是这样的:
<figure class="op-interactive">
<iframe>
<div>
</div>
<script async="" height="" src="src" width=""></script>
</iframe>
</figure>
我不知道如何在script
标签之间插入div
标签,
我的代码中缺少什么吗?
我正在使用此页面分享我的测试:http://sandbox.onlinephpfunctions.com/code/00c285949a467fe44d4a5ce2ed002e6b0ef670a6
能请你帮我吗? 谢谢
答案 0 :(得分:0)
您要将脚本追加到$newiFrame
尝试将其添加到div中:
$newDiv->appendChild($scripts);
答案 1 :(得分:0)
我找到了一种方法:
$body1='<div class="media_embed"><script async="" height="" src="src"></script></div>';
$htmlEncoded = mb_convert_encoding($body1, 'HTML-ENTITIES', 'UTF-8');
$doc = new DOMDocument;
$opcionesLibXML = LIBXML_COMPACT | LIBXML_HTML_NODEFDTD;
@$doc->loadHTML($htmlEncoded, $opcionesLibXML);
$media_embed = $doc->getElementsByTagName('div');
foreach ($media_embed as $media){
$scriptstag = $media->getElementsByTagName('script');
foreach ($scriptstag as $scripts) {
$iframe = $doc->createElement('iframe');
$div = $doc->createElement('div');
$newScript = $doc->createElement('newscript');
$newScript->setAttribute('src', $scripts->getAttribute('src'));
$figure = $doc->createElement('figure');
$figure->setAttribute('class','op-interactive');
$figure->appendChild($iframe);
$iframe->appendChild($div);
$div->appendChild($newScript);
$scripts->parentNode->replaceChild($figure, $scripts);
}
}
$body1 = $doc->saveHTML($doc->documentElement);
$body1 = str_replace('<newscript', '<script', $body1);
$body1 = str_replace('</newscript>', '</script>', $body1);
echo $body1;
但我不知道为什么要使用:
$newScript = $doc->createElement('newscript');
我收到此错误:
Fatal error: Maximum execution time of 3 seconds exceeded
为什么我使用newscript
然后替换那个标签
http://sandbox.onlinephpfunctions.com/code/e0c42f2b9823bf41e92267af89b770848682215a