Skip to content

Instantly share code, notes, and snippets.

@timint
Last active November 30, 2018 01:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timint/509b5f845144478c9ba51763e0f217cf to your computer and use it in GitHub Desktop.
Save timint/509b5f845144478c9ba51763e0f217cf to your computer and use it in GitHub Desktop.
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