All of the sudden, we are experiencing problems with our RSS feed which comes with the Drupal core 7.x . It is no longer valid due to two spaces before the opening XML tag. The following error is what I get in the feed validator:

line 1, column 2: XML parsing error: <unknown>:1:2: XML or text declaration not at start of entity

So I am aware of other related posts on Stack Exchange and tried the things mentioned in:

These are the things we did do so far:

  • Checked all PHP-files on the website for containing blank spaces before the opening tags. There were a few, we fixed them all but no help.
  • Changed the default theme to another theme, making sure it is not theme related.
  • When I create a custom RSS feed with Views, the same two blank spaces show up before the XML opening tag.
  • I checked every page, block and view that has manually written PHP tags everywhere and deleted every space that could cause this.
  • All modules and Drupal core is updated to the most recent versions.
  • I checked on the Drupal forum for related issues in XML Sitemap, Metatag, RDF, Schema.org (modules we are using) with no success. I disabled every one of the above mentioned modules one by one, but with no success.
  • Eventually we exported the complete website, except the sites/default/files/ folder which contains no PHP files anyway, and imported it local. The blank spaces are gone in the local setup.

What makes it a bit difficult too, is that I have no idea when the RSS feed went broken. It can be between a week and a few months, making it difficult to filter out when I updated some contrib modules.

So we are quite desperate in finding this, especially since the problem doesn't exist in a local setup. We have a news website so having a working RSS feed is pretty important.

We must be overlooking something, but have no idea what it can be.


3 Answers 3


Some more troubleshooting ideas:

  • Do the spaces only appear on the RSS page, or also on other pages? That could tell you whether the problem is in a central place or a template/include-file that is only loaded for the RSS feed.

  • You can use var_dump(get_included_files()) (ideally in the template, just before the XML is generated) to see a list of all PHP files that have been included so far. This might help with investigating - look for closing PHP tags at the end of the files, print statements, etc.

Also, an idea for a dirty workaround (may cause other problems, but could be worth a try if it's urgent): ob_start() buffers all output, while ob_end_clean() deletes it. So if you put an ob_start() in the top of your index.php and an ob_end_clean() at the start of the RSS template, this might let you discard the spaces. (It shouldn't affect anything else, because the buffer is flushed automatically if it's still open when the script ends.)

  • I managed to solve the problem. The issue was a module which had two blank spaces AFTER the closing PHP-tags in its .module file. All the topics I've found on the internet only mentioned blank space before the opening PHP-tags, so if anyone else ever have this issue, also look in the closing tags.
    – Apoc
    Nov 26, 2017 at 12:14
  • 4
    Yeah, this is definitely a problem to watch out for. Drupal coding standards actually say to omit the closing PHP tag entirely: It's optional, and removing it reduces avoids the risk of whitespace issues. Nov 26, 2017 at 13:10

To debug whitespaces in the page output, you can use XDebug PHP extension. Here are the suggested options in your XDebug PHP's ini file:

; Data
xdebug.default_enable = 1           ; bool: The stacktraces will be shown by default on an error event.
xdebug.collect_vars = 1             ; bool: Gather information about which variables are used in a certain scope.
xdebug.show_local_vars=1            ; int: Generate stack dumps in error situations.
xdebug.collect_assignments=1        ; bool: Controls whether Xdebug should add variable assignments to function traces.
xdebug.collect_params=4             ; int1-4: Collect the parameters passed to functions when a function call is recorded.
xdebug.collect_return=1             ; bool: Write the return value of function calls to the trace files.
xdebug.var_display_max_children=256 ; int: Amount of array children and object's properties are shown.
xdebug.var_display_max_data=1024    ; int: Max string length that is shown when variables are displayed.
xdebug.var_display_max_depth=5      ; int: How many nested levels of array/object elements are displayed.
xdebug.show_mem_delta=0             ; int: Show the difference in memory usage between function calls.
xdebug.show_exception_trace=0       ; bool: Show a stack trace whenever an exception is raised.
xdebug.max_nesting_level=256        ; int: Controls the protection mechanism for infinite recursion protection.

Then either use step-by-step debugging mode, or temporary activate xdebug.auto_trace=1 to generate trace files within your xdebug.trace_output_dir on every request.

Then request XML file and check the trace file. It'll take a while to track down the source of it by checking the returns of certain functions near end, but at least it's possible.

Alternatively disable as many modules as you can and you can find the problem by trial and error.

If you believe one of your .module's file could consist trailing spaces, the following ex command (part of Vim) will remove any of these:

ex +'bufdo!%s/\s\+$//e' -scxa **/*.*

Note: If your shell supports a new globbing option, use ** for recursive scan. Enable by: shopt -s globstar.

Another way is to run your files through the coding standards using Coder module. After installation, the following command will find the issue related hidden trailing spaces:

phpcs --standard=Drupal some_folder/

To fix the issue in-place, try this one:

phpcbf --standard=Drupal some_folder/

Note: Before run, make sure you've everything committed or made a backup, as above command will reformat all your files.


The issue was a module which had two blank spaces AFTER the closing PHP-tags in its .module file. All the topics I've found on the internet only mentioned blank space before the opening PHP-tags, so if anyone else ever have this issue, also look in the closing tags

  • 3
    .module files are not suppose to have closing tags.
    – No Sssweat
    Nov 27, 2017 at 7:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.