I have a large collection of php files written over the years and I need to properly replace all the short open tags into proper explicit open tags.
change \
PHP 7.4 officially deprecates short open tags and PHP 8 removes them altogether, so this question on SO is going to get somewhat popular as people look for solutions to convert legacy codebases.
As already noted by other answers, sed doesn't cover all use-cases. The suggested full_opening_tag
PHP-CS-Fixer is very sed-like in its behavior and also doesn't cover all use-cases. Also, at least one tool I found such as the one answer by danorton currently only works when short open tags are enabled, which if you upgraded to PHP 8 via an OS upgrade, you can't easily rollback to 7.x to run such tools. Caveat Emptor very much applies with all of these approaches.
I have written a tool that does not depend on the existence of short open tags (i.e. it works with PHP 8), does not use regular expressions (i.e. it uses token_get_all()
), and also avoids non-short open tags (e.g. <?xml
) and other non-tag scenarios (e.g. PHP strings containing "tags").
https://github.com/cubiclesoft/php-short-open-tag-finder/
The default mode that the tool runs in just finds references and displays them. No files are modified.
In -ask
mode, which is currently the only mode that modifies files, the tool asks whether or not it is okay to replace each set of references on a per-file basis. That is, if there are 500 files with 2,000 total short open tag references, it will only ask 500 times.
Even with file level grouping, the tool is perhaps overly cautious in its approach with making changes. But we are talking about potentially modifying thousands of files on a system in a single day. I don't think complete automation is the right answer here. It only took me a few hours to go through and carefully consider every change spanned across a few thousand files on all the systems I manage with the tool.
I have quite a bit of experience with using token_get_all()
as well as writing token parsers.