Skip to content

Instantly share code, notes, and snippets.

Last active Nov 30, 2018
What would you like to do?
A script to find all PHP files with leading or trailing whitespace
header('Content-Type: text/plain');
$dir_iterator = new RecursiveDirectoryIterator("./");
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $file) {
if (!preg_match('#\.php$#', $file)) continue;
$contents = file_get_contents($file);
if (preg_match('#^\s+<\?php#', $contents)) {
echo $file .' (Whitespace before opening <?php at BOF)'. PHP_EOL;
if (preg_match('#\?>\s+$#', $contents)) {
echo $file .' (Whitespace after closing ?> at EOF)'. PHP_EOL;
echo "$count files scanned" . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment