Skip to content

Instantly share code, notes, and snippets.

@timint
Last active Nov 30, 2018
Embed
What would you like to do?
A script to find all PHP files with leading or trailing whitespace
<?php
header('Content-Type: text/plain');
$dir_iterator = new RecursiveDirectoryIterator("./");
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
$count=0;
foreach ($iterator as $file) {
$count++;
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