A Script To Remove PHP Closing Tags
by Bryan on Jul.14, 2010, under Programming
If you are a PHP developer, you may or may not know that it is becoming popular to omit the closing PHP tag in class files as it’s not necessary and it avoids the possibility of a space at the end of the file causing frustration in various situations. If you’ve ever had to remove these tags from a large number of files, you know how tedious it can be. Well, no more; here is a script to do just that. Just change into the directory containing the files to be stripped, and run ‘php-remove-closing-tag’.
The script makes use of the awk, pcregrep, sed, and wc utilities to get the job done. You may need to install some or all of them to make the script work. If you are a developer, you will probably have all of them installed anyway.
Source Code:
#!/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 ## FILES=$(pcregrep -rnM --include='^.*\.php$' '^\?\>(?=([\s\n]+)?$(?!\n))' .); for MATCH in $FILES; do FILE=`echo $MATCH | awk -F ':' '{print $1}'`; TARGET=`echo $MATCH | awk -F ':' '{print $2}'`; 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; done;
UPDATE:
I discovered a bug in the previous version of the script. I was performing a `head -n 1` on the result of the expression to handle the output of multiple lines from the pcregrep but this was limiting the entire search to one file… doh! I fixed this problem by making the extra space search a positive-lookahead. I have tested the new version thoroughly and assure you that it will work as intended.
My hope for this site is that my thoughts and experiences find their way to those who can make use of them. I've been a professional software developer and amateur photographer since 1999 and an amateur musician for nearly 20 years. I have done a fair-share of paying gigs and teaching, but nowadays, I spend most of my music-related time in my home studio. Most of my writing will fall into the main site categories, but I also enjoy reading, running, and auto-racing, so look out for posts on each of those from time to time. Enjoy.
April 7th, 2011 on 6:24 am
Hey Bryan,
After deciding the best way to rid myself of the dreaded “headers already sent..” PITA was to remove the closing php tags from a whole lot of files.
So I’m glad you came up with your little bash script and that I was able to find it…
It worked straight out of the box.
The really nice feature is to have it report the files and line numbers it’s removed the closing php tags on.
Although I had to install pcregrep and awk, that is no biggy. Now I know of some more tools to perform tricks with!
Thanks again Bryan
Now I can get on and do some work!
Cheers
Tim
April 7th, 2011 on 6:31 am
Tim,
I’m glad you could make use of this script.
Thanks!
Bryan