Replace PHP short open tags with full form in all '.php' files using one command
Nowdays, In the latest versions of web servers, The PHP short open tags are diabled by default, Although we can able to enable it in the 'php.ini' file, Some of the shared hosting servers may be diabled that systax and you have to migrate your code to the standard syntax...
So it is good to avoid the short tag and follow standard tag from now on while coding. But to migrate the old codes, Here is a simple command line for linux which migrates the code in all the '.php' files in the current directory to the standard syntax.
find . -iname '*.php' -type f -print0 |xargs -0 sed -i -e 's/<? /<?php /g' -e 's/<?\/\//<?php \/\//g' -e 's/<?\/\*/<?php \/\*/g' -e 's/<?\=/<?php echo/g'
The above command convers,
"<?" to "<?php"
"<?=" to "<?php echo"
"<?//" to "<?php //"
"<?/*" to "<?php /*"
http://www.plugged.in/linux/linux_help/php-convert-open-tags.html
Written by Arun David
Related protips
3 Responses
I believe you want a space at the end, as in "<?php echo "
Thanks a lot for getting the initial work done, however it will break some code or not catch all the shorthand if any of the following are met:
- you already have the newer format. aka "<?php"
- if tab or newline is used directly after the shorthand
you will need regex lookahead, which sadly plain old sed doesn't support, you'll need ssed for this.
a quick install will fix this though and we can use perl like regexing.
apt-get install ssed
now our pattern
find . -iname '*.php' -type f -print0 | xargs -0 ssed -i -R -e 's/<\?\=/<?php echo /g' -e 's/<\?(?!php)(?!=)/<\?php /g'
This will cover
"<?=" to "<?php echo "
"<?" to "<?php " .... but not "<?php"
Note that none of the above accounts for a string <?xml
that might be updated to be <?php xml
which is undesired.