du8791069 2016-02-13 16:10
浏览 95
已采纳

正则表达式删除文件中的最后一个PHP标记

I'm updating some old code, and noticed there's a lot of files that still have the old-style PHP file ending, where the ?> is the last chars in the file.

NOTE: this is in keeping with many coding standards for PHP eg.: http://www.php-fig.org/psr/psr-2/

Is there a way to quickly remove these directory wide (via preg_replace, maybe grep / sed, or whatever) but not remove legitimate closing tags in in-line PHP blocks?

I have no problem finding / replacing these-- I am not sure how to ensure that it's the last characters in the file though.

Thanks

  • 写回答

2条回答

  • dtng25909 2016-02-13 16:51
    关注

    Firstly, as with all bulk search and replace tasks, be sure you have backed up the files so you have something to fall back on in case this doesn't work.

    Then, on the command line, try:

    $ sed -i '$s/\([[:blank:]]\)*?>\([[:blank:]]\)*$//g' "$(grep -rl '^\([[:blank:]]\)*?>\([[:blank:]]\)*$')"
    
    • uses command substitution $() containing a grep command
    • grep -r to recursively find files. If your directory has mixed in non-PHP files and takes too long, we can work in a find, however for now try this
    • grep -l to just list, don't show the match, thus pass file names to sed command
    • [:blank:] is a POSIX character class to match space or tabs.
    • so the grep matches for a line beginning with space or tab for zero+ characters, the ?>, followed by space or tab zero+ characters, then end of line.
    • this is to deal with edge cases where the code does not end as expected with just ?> but for whatever odd reason you happen to have extra white spaces before and after the ?>
    • this grep alone will also include unwanted results where you happen to have ?> on its own line in the middle of the PHP script, so to focus only on end-of-file, last line, we have the sed
    • -i replaces in place. Could also have used -i.bak to automatically have sed create a *.bak file backup, but I prefer not to clutter the web server with *.bak files, and if you followed my recommendation to backup prior to this, you already have a backup and won't need this
    • the sed command starting with $ specifies the address is the last line
    • then the action to take part on that address is a replacement similar to what grep was looking for
    • the sed acts via a replacement, so will still leave a blank line, which at least ensures conformity with PSR-2 All PHP files MUST end with a single blank line. requirement
    • if you aren't getting any fixes at all, it could be DOS vs Linux line ending issues preventing grep from working, in which you may need to use dos2unix on the PHP files and then re-try this command

    The result is the successful elimination of last line ?> , even if there were extra "unclean" spaces before or after ?>.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记