我想将 iframe 放置在正文标记开头的正下方。这有一些问题,因为 body 标签可以有各种属性和奇怪的空格。我的猜测是这将需要正则表达式才能正确执行。
编辑:此解决方案必须与 php 4 配合使用,性能是我关心的问题。就是为了这个 http://drupal.org/node/586210#comment-2567398
最佳答案
您可以使用 DOMDocument和 friend 。假设您有一个变量 html
包含作为字符串的现有 HTML 文档,基本代码是:
$doc = new DOMDocument();
$doc->loadHTML(html);
$body = $doc->getElementsByTagName('body')->item(0);
$iframe = $doc->createElement('iframe');
$body->insertBefore($iframe, $body->firstChild);
要检索修改后的 HTML 文本,请使用
$html = $doc->saveHTML();
编辑:对于 PHP4,您可以尝试 DOM XML .
关于html - PHP : Inject iframe right after body tag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2216224/