我想從完整的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 ... –