Skip to content

Instantly share code, notes, and snippets.

@radmiraal
Last active December 25, 2015 09:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radmiraal/fcf1f68e8e72e0ad1aa3 to your computer and use it in GitHub Desktop.
Save radmiraal/fcf1f68e8e72e0ad1aa3 to your computer and use it in GitHub Desktop.
#!/bin/bash
# vim:ft=sh:ts=3:sts=3:sw=3:et:
###
# Strips the closing php tag `?>` and any following blank lines from the
# end of any PHP file in the current working directory and sub-directories. Files
# with non-whitespace characters following the closing tag will not be affected.
#
# Author: Bryan C. Geraghty <bryan@ravensight.org>
# Date: 2009-10-28
##
if [ -z "$1" ]; then
echo "No path given, script usage: ./clean.sh <path>";
exit 1;
fi
FILES=$(find $1 -name '*.php');
for FILE in $FILES;
do
MATCH=`pcregrep -rnM '^(\n|\?\>)(?=([\s\n]+)?$(?!\n))' $FILE`
if [ "$MATCH" != "" ]; then
TARGET=`echo $MATCH | awk -F ':' '{print $1}'`;
LINE_COUNT=`wc -l $FILE | awk -F " " '{print $1}'`;
echo "Removing lines ${TARGET} through ${LINE_COUNT} from file $FILE...";
sed -i '' "${TARGET},${LINE_COUNT}d" $FILE;
fi
done;
@lolli42
Copy link

lolli42 commented Oct 12, 2013

Or on shell in one command (this was typo3 cms, #52360):

find . -type d ( -path ./typo3/contrib/idna -o -path ./typo3/contrib/pear -o -path ./typo3/sysext/adodb/adodb -o -path ./typo3/sysext/openid/lib/php-openid -o -path ./typo3/sysext/openid/lib/php-openid/Auth ) -prune -o -name '*.php' -print | while read FILE; do tail -n1 $FILE | grep '^?&gt;$' && sed -i '$ d' $FILE; done

@radmiraal
Copy link
Author

tweaked it a bit to remove empty newlines on the end too

@radmiraal
Copy link
Author

@lolli42: thanks ;) Too bad that one didn't turn up in my google search ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment