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:
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:
Unwinding that into semi-plain english for clarity:
file_get_contents("htt...")retrieves the output from Bloglines
str_replacethen looks for any bare ampersands in the string, replacing it with the commented version
_eis 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.