Skip to content

Instantly share code, notes, and snippets.

@GreatPotato
Last active December 30, 2015 05:59
Show Gist options
  • Select an option

  • Save GreatPotato/7786746 to your computer and use it in GitHub Desktop.

Select an option

Save GreatPotato/7786746 to your computer and use it in GitHub Desktop.
Minifys and escapes a HTML file ready for deploying via JS
<?php
class Ebook {
public $input = 'ebook.html';
public $output = 'ebook.min.html';
public function __construct($array)
{
foreach($array as $k => $v)
$this->$k = $v;
}
public function generate_file()
{
$content = $this->get_content();
$content = $this->minify_escape($content);
file_put_contents($this->output, $content);
}
private function get_content()
{
return file_get_contents($this->input);
}
private function minify_escape($content)
{
$content = preg_replace(
array(
'/ {2,}/',
'/<!--.*?-->|\t|(?:\r?\n[ \t]*)+/s'
),
array(
' ',
''
),
$content
);
$content = str_replace(
array(
'"',
'<script',
'</script>',
),
array(
'\"',
'<scr"+"ipt',
'<"+"/script>',
),
$content
);
return $content;
}
}
$ebook = new Ebook(array('ebook.html', 'ebook.min.html'));
$ebook->generate_file();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment