PHP:在 body 标记之后注入 iframe

发布于 2024-08-20 22:11:05 字数 275 浏览 4 评论 0原文

我想在 body 标签开始的正下方放置一个 iframe。这存在一些问题,因为 body 标记可以具有各种属性和奇怪的空格。我的猜测是这将需要正则表达式才能正确执行。

编辑:此解决方案必须与 php 4 & 一起使用性能是我关心的问题。这是为了这个 http://drupal.org/node/586210#comment-2567398

I would like to place an iframe right below the start of the body tag. This has some issues since the body tag can have various attributes and odd whitespace. My guess is this will will require regular expressions to do correctly.

EDIT: This solution has to work with php 4 & performance is a concern of mine. It's for this http://drupal.org/node/586210#comment-2567398

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

写给空气的情书 2024-08-27 22:11:05

您可以使用 DOMDocument 和朋友。假设您有一个变量 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

You can use DOMDocument and friends. Assuming you have a variable html containing the existing HTML document as a string, the basic code is:

$doc = new DOMDocument();
$doc->loadHTML(html);
$body = $doc->getElementsByTagName('body')->item(0);
$iframe = $doc->createElement('iframe');
$body->insertBefore($iframe, $body->firstChild);

To retrieve the modified HTML text, use

$html = $doc->saveHTML();

EDIT: For PHP4, you can try DOM XML.

掩于岁月 2024-08-27 22:11:05

PHP 4 和 PHP 5 都应该对 preg_split()

/* split the string contained in $html in three parts: 
 * everything before the <body> tag
 * the body tag with any attributes in it
 * everything following the body tag
 */
$matches = preg_split('/(<body.*?>)/i', $html, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); 

/* assemble the HTML output back with the iframe code in it */
$injectedHTML = $matches[0] . $matches[1] . $iframeCode . $matches[2];

Both PHP 4 and PHP 5 should be happy with preg_split():

/* split the string contained in $html in three parts: 
 * everything before the <body> tag
 * the body tag with any attributes in it
 * everything following the body tag
 */
$matches = preg_split('/(<body.*?>)/i', $html, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); 

/* assemble the HTML output back with the iframe code in it */
$injectedHTML = $matches[0] . $matches[1] . $iframeCode . $matches[2];
明媚殇 2024-08-27 22:11:05

使用正则表达式会带来性能问题......这就是我想要的

<?php
$html = file_get_contents('http://www.yahoo.com/');
$start = stripos($html, '<body');
$end = stripos($html, '>', $start);
$body = substr_replace($html, '<IFRAME INSERT>', $end+1, 0);
echo htmlentities($body);
?>

想法?

Using regular expressions brings up performance concerns... This is what I'm going for

<?php
$html = file_get_contents('http://www.yahoo.com/');
$start = stripos($html, '<body');
$end = stripos($html, '>', $start);
$body = substr_replace($html, '<IFRAME INSERT>', $end+1, 0);
echo htmlentities($body);
?>

Thoughts?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文