结束后找到额外的空间/新行?> (php 标签)
因此,我在结束 ?>
(php 标记)后有一个空格/换行,这破坏了我的应用程序。
我怎样才能轻松找到它 我在这个应用程序中有 1000 个文件和 100000 行代码。
理想情况下,我将一些正则表达式与 find grep 结合起来在 unix 机器上运行。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这里的问题是正常的 grep 不匹配多行。因此,我将安装 pcregrep 并尝试以下命令:
这将使用 PCRE 多行匹配(
-r
部分)来匹配文件夹和子文件夹(-r
部分)中的所有文件>-M 部分),并且只列出它们的文件名(-l
部分)。至于模式,匹配
?>
后跟 1 个或多个空格或换行符,后跟文件末尾\z
。但我发现,当我在文件夹上运行此命令时,许多 PHP 文件实际上以单个换行符结尾。因此,您可以将该正则表达式更新为'\?>[\s\n]+\n\z'
以匹配单个\n
字符终止符。最后,如果您需要检查其确切的字符序列结尾,您始终可以使用 od -c filename 来打印文件的明确表示。
The problem here is normal grep doesn't match multiple lines. So, I would install
pcregrep
and try the following command: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.这可以通过常规 grep
在文件开头搜索空格
-Rl来搜索从当前目录开始的所有文件,并列出所有在文件末尾有
?>
后跟空格的文件。-P
将模式解释为与 Perl 兼容的正则表达式 (PCRE)。-z
将输入文件视为一长行 - 这就是它工作的部分原因[\s]+
匹配至少一个空格 - 包括换行符如果你想要仅匹配 PHP 文件:
在文件开头搜索空格
This is possible with regular grep
To search for white space at the beginning of the file before
-RlWill search for all files starting from the current directory and list all that have a
?>
followed by white space at the end of the file.-P
Interpret the pattern as a Perl-compatible regular expression (PCRE).-z
Treats the input file as one long line - this is in part what makes it work[\s]+
matches at least one white space - including newlinesIf you want to match PHP files only:
To search for white space at the beginning of the file before
使用 Perl;
s/\s*$//s - 处理所有行作为单行并将末尾的任何空格替换为空
use perl;
s/\s*$//s - treat all lines as a single line and substitute any space at the end to nothing
这适用于我的盒子:
想法是只取最后 3 个带有尾部的字符,然后丢弃那些为 '?'、'>' 的文件和换行符。如果有空格或另一个换行符,您将不会得到“?”特点..
This works on my box:
The idea is that you take just the last 3 characters with tail, then discard the files where those are '?', '>' and newline. If there's a space or another newline, you won't get the '?' character..
对每个未完全测试的文件执行..
记住处理临时文件,并在 sed 操作后将新文件复制到原始文件上..
to be executed for each file not completely tested..
remember to work on a temporary file and after the sed operation copy the new file on the original one..
这对我有用...
如果您需要在 sublime-settings 文件或类似的不喜欢正斜杠的文件中使用,您可能需要为每个文件添加一个额外的斜杠,如下所示...
希望有帮助!
This works for me...
If you need to use in in a sublime-settings file or something like that which doesn't like forward slashes, you might have to add an extra slash for each of them like so...
Hope that helps!
这对我在 php 文件之前找到空格很有用
This worked for me to find white spaces before php files
grep '?> > '*.php
?当然,它可能不是空格,也可能是换行符或制表符,因此您可能想尝试其他字符。grep '?> ' *.php
? Of course, it may not be a space and could be a linebreak or a tab, so you may want to try other characters.使用记事本++,您可以轻松地同时替换所有文档,拖放该文件文件夹并按 CTRL + R,您也可以使用正则表达式
Using notepad++ you can replace easily all documents at the same time, drap and drop that folder of files and press CTRL + R, also you can use Regex