How would I split a HTML formatted file into several HTML files (complete with with HTML, HEAD and BODY tags) with PHP? I would have a placeholder tag (something like <div class='placeholder'></div>
) for all the places I want to cut.
Thanks.
How would I split a HTML formatted file into several HTML files (complete with with HTML, HEAD and BODY tags) with PHP? I would have a placeholder tag (something like <div class='placeholder'></div>
) for all the places I want to cut.
Thanks.
$sourceHTML = file_get_contents('sourcefile');
$splitContents = explode("<div class='placeholder'></div>", $sourceHTML);
foreach ($splitContents as $html) {
// save html to file
}
Edit: whoops. As user201140 correctly points out, I missed the fact that each html file has to be a valid document. Since it's not specified exactly what the head tag should contain, I'll assume that the head tag of the combined document should be replicated to each copy. In that case:
$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
}