0
0

More than 1 year has passed since last update.

PHP(CLI)でhtml、css、jsのunify、minify

Posted at
<?php
$contents = file_get_contents('http://localhost:8000/index.html');
$contents = preg_replace('/<!--.*-->/', '', $contents);
{
	preg_match_all('/<link rel="stylesheet" href="(.*\.css).*/', $contents, $matches, PREG_SET_ORDER);
	foreach ($matches as $match) {
		$inner = file_get_contents('./static/'.$match[1]);
		$inner = preg_replace('#/\*([^*]|\*[^/])*\*/#s', '', $inner);
		$inner = trim($inner)."\n";
		$contents = str_replace($match[0], "<style>\n".$inner."\t\t</style>", $contents);
	}
}
{
	preg_match_all('/<script src="(.*\.js).*/', $contents, $matches, PREG_SET_ORDER);
	foreach ($matches as $match) {
		$inner = file_get_contents('./'.$match[1]);
		$inner = preg_replace('#/\*([^*]|\*[^/])*\*/#s', '', $inner);
		$inner = preg_replace('#^\s*//.*#m', '', $inner);
//		$inner = preg_replace('#^\s*console\..*#m', '', $inner);
		$inner = trim($inner)."\n";
		$contents = str_replace($match[0], "<script>\n".$inner."\t\t</script>", $contents);
	}
}
$contents = preg_replace('/\s*\n\s*/', '', $contents);
echo $contents;
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0