且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在结束之后找到额外的空格/新行?> (php标签)

更新时间:2022-10-30 22:35:16

这里的问题是普通的grep不匹配多行。所以,我会安装 pcregrep 并尝试下面的命令:

  pcregrep -rMl'\?> [\s\\\
] + \ z'*

这将使用PCRE多行匹配( -M )匹配文件夹和子文件夹( -r 部分)中的所有文件>部分),并且只列出它们的文件名( -l 部分)。



至于模式,匹配?>> ,后跟1个或多个空格或换行符,接着是文件的结尾 \ z 。我发现,当我在我的文件夹上运行这个文件时,许多PHP文件实际上只有一个换行符。所以你可以把这个正则表达式更新为'\?> [\\\\\\\\\\\\\\\\\\\\\\\]在单个 \\\
字符结束符之上。

最后,您可以使用 od -c filename 打印文件的明确表示,如果您需要检查其确切的字符序列结尾。

So I have a space/new line after a closing ?> (php tag) that is breaking my application.

How can I find it easily I have 1000 of files and 100000 lines of code in this app.

Ideally im after some regex combined with find grep to run on a unix box.

The problem here is normal grep doesn't match multiple lines. So, I would install pcregrep and try the following command:

pcregrep -rMl '\?>[\s\n]+\z' *

This will match all files in the folder and subfolders (the -r part) using PCRE multiline match (the -M part), and only list their filenames (the -l part).

As for the pattern, well that matches ?> followed by 1 or more whitespace or newline characters, followed by the end of the file \z. I found though, when I ran this on my folder, many of the PHP files do in fact end with a single newline. So you can update that regex to be '\?>[\s\n]+\n\z' to match files with whitespace over and above the single \n character terminator.

Lastly, you can always use od -c filename to print unambiguous representation of the file if you need to check its exact character sequence ending.