File_get_contents in url with ampersand (&)

February 2, 2013 0 By Renan W. Silva

After much work to use the file_get_contents command in urls with an ampersand (‘&’ or ampersand) I eventualy found the alternative.

Pass the string that will be submitted to the file_get_contents str_replace. This would be:

str_replace (‘&’, ‘&’, $url);

Do not ask me how it works. But it worked for me.

I reached it the following article

Outside Ampersand Damn!


I’m a bit of a stickler for perfection, and in terms of the interweb, perfection starts with valid XHTML. I just can not help but feel dirty somehow when the almighty validator returns errors. So you can imagine my chagrin when the validator started complaining about a dirty business on my page.

After investigation, it seems said business was included in the output of Bloglines, which I use to display the list little blogroll on the right side of the main page. HTML parsers tend to get upset with a naked, and prefer that one”flight” as the symbol  &  . So getting down and dirty, I had a look at how the output Bloglines was rendered on the page:  
[code] include (“”);

So, thinking I was some kind of god lower PHP, I decided to just run the output through  str_replace  , search and replace with its commercial and tidier escaped versions. No joy. The output Bloglines kept coming right up.

A browse through the manual revealed the PHP  file_get_contents () method for me, which is used to retrieve content from another page in a local variable. I initially shared with plain old  file ()  , but this command retrieves the file into an array instead of a single chain.

Anyway, after a bit of hackery and testing, I have the solution reduced to a

Unwinding that in semi-plain English for clarity:

  • file_get_contents ("htt ...")  retrieves the output Bloglines
  • str_replace  then looks for all commercial and naked in the sequence, replacing it with the annotated version
  • _e  is WordPress’ echo located, that produces the chain on page

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