Skip to content

Instantly share code, notes, and snippets.

@mindbat
Last active December 18, 2015 05:29
Show Gist options
  • Save mindbat/5733086 to your computer and use it in GitHub Desktop.
Save mindbat/5733086 to your computer and use it in GitHub Desktop.
Sed script for re-formatting php files Use: sed -r -f format.sd -i <path/to/file/to/reformat>
# swap tabs for spaces
s/\t/ /g
# ensure spaces around =>
s/([^ ])=>([^ ])/\1 => \2/g
# remove trailing whitespace
s/ +$//g
# ensure spaces around .
s/('|\)|\])\.('|\$|\(|\[)/\1 . \2/g
# handle explode and implode exceptions
s/explode\(' \. '/explode\('\.'/g
s/implode\(' \. '/implode\('\.'/g
# ensure space between if/for/foreach and (
s/(if|for|foreach)\(/\1 (/g
# ensure space for commas
s/(.),([^ ])/\1, \2/g
# convert hash comments to //
s/^([ ]+)\#(.+)/\1 \/\/\2/g
# ensure space after one-line comments
s/^([ ]+)\/\/([^ ])/\1\/\/ \2/g
# implement one true brace
/^[^\*\/]*\b(function|if|else|for|foreach|switch|catch|while|case|do|try)\b([^;{]*)\)([^;{]*)$/,+1 {
/^ *\{ *$/d
s/([^\{]*)/\1 \{/g
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment