$sourceHTML = file_get_contents('sourcefile');
$splitContents = explode("<div class='placeholder'></div>", $sourceHTML);
foreach ($splitContents as $html) {
// save html to file
}
Run Code Online (Sandbox Code Playgroud)
编辑:哎呀。正如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
}
Run Code Online (Sandbox Code Playgroud)