Skip to content

Instantly share code, notes, and snippets.

@arian
Created September 7, 2010 18:57
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 arian/568853 to your computer and use it in GitHub Desktop.
Save arian/568853 to your computer and use it in GitHub Desktop.
Trim all trailing whitespace
#!/usr/bin/env php
<?php
$files = explode("\n", shell_exec('git ls-files'));
foreach ($files as $file){
$file = trim(__DIR__ . '/' . $file);
if (!(substr($file, -4) == 'html' || substr($file, -2) == 'js')) continue;
$content = file_get_contents($file);
// trim trailing whitespace
$content = preg_replace('/[ \t]*$/m', '', $content);
// `) {` -> `){`
$content = preg_replace('/\) \{(\s)/', '){$1', $content);
$content = str_replace(array(
'if(',
'for(',
'while(',
'switch(',
), array(
'if (',
'for (',
'while (',
'switch (',
), $content);
file_put_contents($file, $content);
echo 'Cleaned: ' .$file . PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment