I have an error in my WordPress website (XML parsing error) because there is a blank line before the <DOCTYPE>
. Probably this is caused by a blank line in one of the theme or plugin files before the PHP opening tag <?php
or after the closing tag ?>
. I already checked some files (theme index.php
, header.php
, functions.php
and a few plugins) but did not found the cause.
Is there a smart trick to check all files for any blank lines before or after the php tags? Some Regex maybe? Or otherwise any method to check which theme file or plugin file outputs this line?
I don't think that just
at top of the file is the problem. Those whitespace characters are usually ignored.
I suppose that you have created the files as UTF-8 encoded files with byte order mark (BOM) at beginning. Text editors and IDEs do not display the BOM of a Unicode encoded file.
The UTF-8 BOM is 0xEF 0xBB 0xBF displayed with Windows-1252 code page as  if text editors would display them. The text editor UltraEdit allows to override the automatic Unicode detection on using File - Open and selecting in the file opening dialog ASCII on Open as option to open a UTF-8 encoded file as ASCII/ANSI file. Than the UTF-8 BOM at beginning of an UTF-8 encoded Unicode file with BOM can be seen also in text editing mode.
A very simple search to find files with a UTF-8 BOM at top is searching for files containing the string

. Or if you do not want to depend on a code page, run a Perl regular expression search with the expression\xEF\xBB\xBF
.Using an empty string as replace string should result in removing the UTF-8 BOM from all files.
\R
can be used to match a DOS/Windows or UNIX or MAC line termination. In other words\R
is equivalent to(?:\r\n|\n|\r)
or shorter(?:\r?\n|\r)
However, because of my byte order mark suspicion I suggest to use as search string
Explanation:
(?:
...)
... a non marking group for the OR expression.\xEF\xBB\xBF\s*
... a UTF-8 BOM with zero or more whitespaces appended.|
... means OR.\s+
... a whitespace character one or more times.(?=<\?php)
... a positive lookahead to check if the next characters are<?php
without really matching them.That search string is not limited to beginning of a file. But perhaps it is nevertheless enough for your needs to find files with a UTF-8 BOM or with a blank line at beginning of a PHP file.