Last active
December 16, 2015 00:29
-
-
Save jflefebvre/5348124 to your computer and use it in GitHub Desktop.
Filter php source file and removes comments, white spaces, crlf ...Used to pack my reverse script file.-- still some minor fixes to do in the future but the main idea is implemented :)
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
#!/usr/bin/env php | |
<?php | |
/** | |
* Take a source file and strip all comments, spaces, crlf | |
* | |
* Author: Jean-François Lefebvre | |
* E-mail: lefebvre.jf@gmail.com | |
* | |
*/ | |
const sourceFilename = 'reverse.php'; // --- php script filename | |
const targetFilename = 'reverse'; // --- target file | |
$content=file_get_contents(__DIR__.'/'.$sourceFilename); | |
// --- extract comment blocks and remove them from source code | |
$commentBlocks = array_filter(token_get_all($content), | |
function($item) { | |
return $item[0] == T_DOC_COMMENT; | |
} | |
); | |
foreach ($commentBlocks as $commentBlock) { | |
$content = str_replace($commentBlock[1], '', $content); // --- remove comment block | |
} | |
$content = str_replace("\r", '', $content); // --- replace with space | |
$content = str_replace("\n", ' ', $content); // --- replace with space | |
$content = str_replace("\t", ' ', $content); // --- replace tabs with space | |
$string = trim(preg_replace('/ {2,}/', ' ', $content)); // --- remove multiple spaces | |
$content = str_replace('<?php', PHP_EOL.'<?php ', $content); // --- add a newline and add a php start tag | |
file_put_contents(__DIR__.'/'.$targetFilename, $content); // --- write target file | |
chmod(__DIR__.'/'.$targetFilename, 0755); // --- set as executable |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment