Created
June 20, 2017 05:51
-
-
Save brankoajzele/7d02f7f05b78fbd32f885b6a87894e4b to your computer and use it in GitHub Desktop.
extract-code.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(E_ALL); | |
ini_set('display_errors', 1); | |
set_time_limit(0); | |
$chapters = glob('./chapters/*.html'); | |
$dom = new DOMDocument; | |
foreach ($chapters as $chapter) { | |
// Extract this chapter name, without path and suffix info | |
$name = basename($chapter, '.html'); | |
// Silence invalid HTML format warnings | |
// https://stackoverflow.com/a/17559716 | |
libxml_use_internal_errors(true); | |
$dom->loadHTMLFile($chapter); | |
libxml_clear_errors(); | |
// Get all <pre> elements | |
$codes = $dom->getElementsByTagName('pre'); | |
// Set counter for this chapter's codes | |
$counter = 0; | |
// Loop through each chapter code and extract it into its own file | |
foreach ($codes as $code) { | |
$counter++; | |
file_put_contents( | |
'./chapters-codes/' . $name . '_' . $counter . '.php', | |
$code->nodeValue | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment