Last active
December 18, 2015 05:29
-
-
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>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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