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

发布于 2024-11-10 03:06:16 字数 168 浏览 1 评论 0原文

因此,我在结束 ?> (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

挽你眉间 2024-11-17 03:06:16

这里的问题是正常的 grep 不匹配多行。因此,我将安装 pcregrep 并尝试以下命令:

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

这将使用 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:

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.

萌︼了一个春 2024-11-17 03:06:16

这可以通过常规 grep

grep -Pz '\?>[\s]+

来搜索从当前目录开始的所有文件,并列出所有在文件末尾有 ?> 后跟空格的文件。

  • -P 将模式解释为与 Perl 兼容的正则表达式 (PCRE)。
  • -z 将输入文件视为一长行 - 这就是它工作的部分原因
  • [\s]+ 匹配至少一个空格 - 包括换行符

如果你想要仅匹配 PHP 文件:

find -name '*.php' | xargs grep -Pz '\?>[\s]+

在文件开头搜索空格

find -name '*.php' | xargs grep -Pz '^[\s]+<\?' -l
-Rl

来搜索从当前目录开始的所有文件,并列出所有在文件末尾有 ?> 后跟空格的文件。

  • -P 将模式解释为与 Perl 兼容的正则表达式 (PCRE)。
  • -z 将输入文件视为一长行 - 这就是它工作的部分原因
  • [\s]+ 匹配至少一个空格 - 包括换行符

如果你想要仅匹配 PHP 文件:


在文件开头搜索空格


 -l

在文件开头搜索空格

-Rl

来搜索从当前目录开始的所有文件,并列出所有在文件末尾有 ?> 后跟空格的文件。

  • -P 将模式解释为与 Perl 兼容的正则表达式 (PCRE)。
  • -z 将输入文件视为一长行 - 这就是它工作的部分原因
  • [\s]+ 匹配至少一个空格 - 包括换行符

如果你想要仅匹配 PHP 文件:

在文件开头搜索空格

This is possible with regular grep

grep -Pz '\?>[\s]+

Will 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 newlines

If you want to match PHP files only:

find -name '*.php' | xargs grep -Pz '\?>[\s]+

To search for white space at the beginning of the file before

find -name '*.php' | xargs grep -Pz '^[\s]+<\?' -l
-Rl

Will 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 newlines

If you want to match PHP files only:


To search for white space at the beginning of the file before


 -l

To search for white space at the beginning of the file before

-Rl

Will 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 newlines

If you want to match PHP files only:

To search for white space at the beginning of the file before

娇女薄笑 2024-11-17 03:06:16

使用 Perl;

perl -0777 -i -pe 's/\s*$//s' *.php
  • -0777 将吞掉整个文件(-0 也可以)
  • -i - 就地编辑,因此文件将被结果替换
  • -p 打印行
  • -e perl 表达式

s/\s*$//s - 处理所有行作为单行并将末尾的任何空格替换为空

use perl;

perl -0777 -i -pe 's/\s*$//s' *.php
  • -0777 will slurp he whole file (-0 will be ok too)
  • -i - inplace editing, so the file will be replaces with the result
  • -p print lines
  • -e perl expression

s/\s*$//s - treat all lines as a single line and substitute any space at the end to nothing

冰之心 2024-11-17 03:06:16

这适用于我的盒子:

for i in `find . -name "*.php"`; do (echo -n "$i: "; tail -c 3 $i) | grep -v "[?]>"; done

想法是只取最后 3 个带有尾部的字符,然后丢弃那些为 '?'、'>' 的文件和换行符。如果有空格或另一个换行符,您将不会得到“?”特点..

This works on my box:

for i in `find . -name "*.php"`; do (echo -n "$i: "; tail -c 3 $i) | grep -v "[?]>"; done

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

§对你不离不弃 2024-11-17 03:06:16
sed -e :a -e '/^[ \n]*$/{$d;N;ba' -e '}' -e '$ s/\([^ ]\)*?>[ ]*/\1?>/' file.php > new_file.php

对每个未完全测试的文件执行..

记住处理临时文件,并在 sed 操作后将新文件复制到原始文件上..

sed -e :a -e '/^[ \n]*$/{$d;N;ba' -e '}' -e '$ s/\([^ ]\)*?>[ ]*/\1?>/' file.php > new_file.php

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

咆哮 2024-11-17 03:06:16

这对我有用...

<\?php | \?>

如果您需要在 sublime-settings 文件或类似的不喜欢正斜杠的文件中使用,您可能需要为每个文件添加一个额外的斜杠,如下所示...

<\\?php | \\?>

希望有帮助!

This works for me...

<\?php | \?>

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

<\\?php | \\?>

Hope that helps!

沫雨熙 2024-11-17 03:06:16

这对我在 php 文件之前找到空格很有用

find -name '*.php' | xargs grep -Pz '\?>[\s]+
 -l

This worked for me to find white spaces before php files

find -name '*.php' | xargs grep -Pz '\?>[\s]+
 -l
今天小雨转甜 2024-11-17 03:06:16

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.

眼泪都笑了 2024-11-17 03:06:16

使用记事本++,您可以轻松地同时替换所有文档,拖放该文件文件夹并按 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文