Find extra space / new line after a closing ?> (php tag)

后端 未结 9 1701
醉话见心
醉话见心 2020-12-17 18:04

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 lin

9条回答
  •  盖世英雄少女心
    2020-12-17 19:04

    This is possible with regular grep

    grep -Pz '\?>[\s]+$' -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:

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

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

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

提交回复
热议问题