Fixing "Whitespace is not allowed at this location" Bugs With PHP
Have you ever opened up an RSS feed and been greeted with the following error?
Whitespace is not allowed at this location.
Line: X Character: X
This is usually because your RSS feed features characters which stop it from properly parsing feed. Characters like, &, <, >, etc are usually to blame, they are known as "reserved characters".
If your RSS feed is pulling content from your blog for instance, one way of fixing the issue is simply editing posts that use those characters and remove them. This might work if you have a post or two with the following issue, but what if you have dozens?
Using PHP, you can fix the issue pretty easily. The first thing you want to do is open up your RSS feed and find where things like the name of the post, URL, etc are generated. With OneCMS, the code looks something like this,
[code]echo "";[/code]
In my posts, if I was having & issues for instance, I can use str_replace() to fix them. While the RSS feed can't directly parse & characters there are work arounds as noted in the above link on reserved characters. The new code would look something like this,
[code]echo "";[/code]
This tells the script to find all instances of "&" and replace them with "&", when you load the RSS feed in your browser, the & is automatically converted into an &. Alternatively you can edit the second "" and replace it with what ever you want. If you want an - to show for example, you simply change it to "-".
If there are multiple objects potentially interfering with your RSS feed, I recommend creating a function to deal with the issue. It can look something like this.
(Note: I haven't tested the following function, it's simply included to give you a brief idea of how something like this can work.)
[code]function rssclean($var) {
$name = str_replace(""", """, $name);
$name = str_replace("'", "'", $name);
$name = str_replace("&", "&", $name);
$name = str_replace("<", "<", $name);
$name = str_replace(">", ">", $name);
}
[/code]
Using our code from above for example, we would call this function using the following method.
[code]echo "";[/code]
Again, keep in mind, I haven't tested the function so use at your own risk.
This should help you deal with whitespace RSS problems, if you have any questions, comments, etc please leave them below!