Batch script to replace PHP short open tags with <?php

前端 未结 13 1739
轻奢々
轻奢々 2020-11-30 01:49

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 \         


        
相关标签:
13条回答
  • 2020-11-30 02:56

    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.

    0 讨论(0)
提交回复
热议问题