Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save brankoajzele/7d02f7f05b78fbd32f885b6a87894e4b to your computer and use it in GitHub Desktop.
Save brankoajzele/7d02f7f05b78fbd32f885b6a87894e4b to your computer and use it in GitHub Desktop.
extract-code.php
<?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