如何使用PHP将HTML格式的文件拆分为多个HTML文件(包含HTML,HEAD和BODY标记)?对于我想剪切的所有地方,我都会有一个占位符标记(类似<div class='placeholder'></div>
)。
感谢。
答案 0 :(得分:4)
$sourceHTML = file_get_contents('sourcefile');
$splitContents = explode("<div class='placeholder'></div>", $sourceHTML);
foreach ($splitContents as $html) {
// save html to file
}
编辑:哎呀。正如user201140正确指出的那样,我错过了每个html文件必须是有效文档的事实。由于未指定head标记应包含的内容,我将假设组合文档的head标记应复制到每个副本。在那种情况下:
$sourceHTML = file_get_contents('sourcefile');
preg_match("/(^.*<body.*?>)(.*)(<\/body.*$)/is", $sourceHTML, &$matches);
$top = $matches[1];
$contents = $matches[2];
$bottom = $matches[3];
$splitContents = explode("<div class='placeholder'></div>", $contents);
foreach ($splitContents as $chunk) {
$html = $top.$chunk.$bottom;
// save html to file
}
答案 1 :(得分:0)
preg似乎只适用于小文件...
无论如何......要分割此表单的HTML文件:
(header...) <body><div class='container'> (intro...)
<h3>Sect 1</h3> (section...)
<h3>Sect 2</h3> (section...)
(etc...)
</div></body></html>
我这样管理:
$splitContents = explode("<h3", $sourceHTML);
$i=0;
$last=count($splitContents)-1;
foreach ($splitContents as $chunk) {
if($i==0) {
$beginning=explode("<body", $chunk);
$top=$beginning[0];
$html = $chunk ;
} else {
$html = $top . "<body><div class='container'><h3" . $chunk ;
}
if($i !=$last) $html .= "</div></body></html>";
// save html to file
++$i;
}