Skip navigation

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.

Download Source

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.

3 Comments

  1. 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

  2. Tim,

    I’m glad you could make use of this script.

    Thanks!
    Bryan

  3. Mac users: Use brew to install gnu-sed and pcre to get this script to work. Make sure you use –default-names on gnu-sed and add the suggested include path for GNU software if you haven’t previously done so.


Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">