Out Damn Ampersand!

I’m a bit of a stickler for perfection, and in terms of the interweb, perfection starts with valid XHTML. I just can’t help but feel dirty in some way when the all powerful validator returns errors. So you can imagine my chagrin when the validator started complaining about a dirty ampersand in my page.

Upon investigation, it turns out said ampersand was included in the output from Bloglines, which I use to display the little blogroll list on the right hand side of the main page. HTML parsers tend to get upset at a bare &, and prefer that one ‘escape’ the symbol as &. So getting down and dirty, I had a look at how the Bloglines output was processed in the page:
[code]include (“http://rpc.bloglines.com/blogroll?html=1&id=gadgetophile”);

Then, thinking I was some sort of lesser PHP god, I decided to just run the output through str_replace, searching and replacing ampersands with their tidier escaped versions. No joy. The Bloglines output kept coming straight through.

A browse through the PHP manual revealed the file_get_contents() method to me, which is used for retrieving the contents of another page into a local variable. I’d initially dabbled with plain old file(), but that command retrieves the file into an array, rather than a single string.

Anyway, after a bit of hackery and testing, I got the solution pared down to one line:
[code]_e(str_replace(‘&’,’&’,file_get_contents(“http://rpc.bloglines.com/blogroll?html=1&id=gadgetophile”)));

Unwinding that into semi-plain english for clarity:

  • file_get_contents("htt...") retrieves the output from Bloglines
  • str_replace then looks for any bare ampersands in the string, replacing it with the commented version
  • _e is WordPress’ localised echo command, which outputs the string to the page

I’m hoping this is the most efficient way, but if any actual PHP gurus can tell me otherwise, fire away in the comments.

2 Replies to “Out Damn Ampersand!”

  1. NOW look what you’ve done! “after a bit of hackery and testing” you’ve not only broken the Interweb – my email system has gone all bung too… spitting out nonsense like “This week’s edition of the Molesworth & Featherston newsletter is attached.” Thanks. Very Much. 😉

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.