正则表达式删除文件中的最后一个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

展开翻译

译文

我正在更新一些旧代码,并注意到有很多文件仍然具有旧式PHP文件 结束,其中?&gt; </ code>是文件中的最后一个字符。</ p>

注意:这符合PHP的许多编码标准,例如: http://www.php-fig.org/psr/psr-2/ </ p>

有没有办法快速删除这些目录范围(通过 preg_replace </ code>,也许 grep </ code> / sed </ code> ,或者其他什么)但不删除内嵌PHP块中的合法结束标记?</ p>

我没有问题查找/替换这些 - 我不知道如何确保它是最后一个字符 但是在文件中。</ p>

谢谢</ p>
</ div>

douou5933
douou5933 找到'/\?>\s*\z/'替换
接近 4 年之前 回复
dp152153
dp152153 对于GNUsed的一个文件:sed-i'$s/?>$//'文件
接近 4 年之前 回复
dpsfay2510
dpsfay2510 感谢您的意见。然而,这与问题无关。
接近 4 年之前 回复
douyan1972
douyan1972 我从未说过我被“强迫”删除它们。它正在更新为更现代的编码风格
接近 4 年之前 回复
douli1854
douli1854 哪个php版本强制你删除它们?
接近 4 年之前 回复

2个回答

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 ?>.

doujing5846
doujing5846 1表示免责声明和备用说明
接近 4 年之前 回复

I faced very similar issue recently. In order to fix this I decided to replace content of .php files in Notepad++.

1) Firstly backup all your files in working directory 2) Secondly use following regex in notepad++ to replace all .php files:

(?s)\A(\s+)?<\?(php)?(.*?)\?>(\s+)?\Z

Replace it with:

<?\2\3

It matches all files starting with <? or <?php and ending with ?>. Additionally it removes white spaces before and after tags. If you do not want this behavior to appear, remove the (\s+)? part from regex.

\A - means beginning of file \s - matching white spaces \Z - means end of file .*? - match everything between tags

展开翻译

译文



我最近遇到了非常类似的问题。 为了解决这个问题,我决定在Notepad ++中替换.php文件的内容。</ p>

1)首先备份工作目录中的所有文件
2)然后在notepad ++中使用以下正则表达式替换所有文件 .php文件:</ p>

 (?s)\ A(\ s +)?&lt; \?(php)?(。*?)\?&gt;(\ s +)  ?\ Z 
</ code> </ pre>

将其替换为:</ p>

 &lt;?\ 2 \ 3 
</ code > </ pre>

它匹配所有以&lt;?</ code>或&lt;?php </ code>开头并以结尾的文件?&gt; < /code>.
另外它会删除标签前后的空格。 如果您不希望出现此行为,请从正则表达式中删除(\ s +)?</ code>部分。</ p>

\ A - 表示文件的开头
\ s - 匹配空格
\ Z - 表示文件结尾
。*? - 匹配标签之间的所有内容</ p>
</ div>

dongtuo2373
dongtuo2373 优秀解决方案
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!