Find all PHP Short Tag instances – COMMANDLINE
Occassionally we have run across web products which were developed using the PHP short open tag “<?” instead of “<?php”.
We could go into the php.ini file and update “short_open_tag” to “On”, however this ends up creating software which can not run on as many servers, and it is less transportable between servers.
The command below when run from the directory that houses all of your PHP files, will identify all of the files which use short open tags. You will then be able to make the changes to the files from <? to <?php
grep -rI '<?' -n . |grep -v '<?[(php)(xml)="]'
This command is running a first grep statement recursively in the current directory looking for any “<?”. The output of this is passed through another grep statement which then ignores any instances of “<?php”, “<?xml”, “<?=” and ‘<?”‘
Lets decompose:
- -r – means search the current (“.”) directory recursively
- -I means ignore binary files
- ‘<?’ search for all instances of ‘<?’
- -n – add the line number of the found code to help you find it faster
- -v – in the excludes anythign that matches in the second grep statement
- ‘ the regular expression then matches each of the items we want to ignore.
Note:
I have put in double quote(“) in the regular expression which ignores <?” because we have some php functions which loop through some XML code and tests for “<?”.