【发布时间】:2014-08-16 10:08:02
【问题描述】:
我想从完整的 html 代码中去除一些 html-body 代码。
我使用下面的脚本。
<?php
function getbody($filename) {
$file = file_get_contents($filename);
$bodystartpattern = ".*<body>";
$bodyendpattern = "</body>.*";
$noheader = eregi_replace($bodystartpattern, "", $file);
$noheader = eregi_replace($bodyendpattern, "", $noheader);
return $noheader;
}
$bodycontent = getbody($_GET['url']);
?>
但在某些情况下,标签<body> 并不存在,但标签可能是<body style="margin:0;"> 或其他东西。谁能告诉我,在这种情况下,通过在 $bodystartpattern 中使用正则表达式来查找开头-body-tag 的关闭->”,找到 body-tag 的解决方案是什么?
【问题讨论】:
-
旁注:
eregi_replace()自 PHP 5.3.0 起,该函数已被弃用。强烈建议不要依赖此功能。 -
检查this answer 使用正则表达式解析html...