我想从完整的html代码中剥离一些html-body代码。php从html页面获取正文
我使用下面的脚本。
<?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标签的关闭 - “>”?
旁注:['eregi_replace()'](http://www.php.net//manual/en/function.eregi-replace.php)该函数已被弃用的PHP 5.3.0 。依靠这个功能是非常不鼓励的。 –
检查[这个答案](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags#answer-1732454)使用正则表达式来解析HTML ... –