PHP escape quotes and urlencode for facebook sharer link

1.8k views Asked by At

I use Facebook sharer links on my website to publish articles on Facebook.

For some links it works and for others, it doesn't. The titles and texts that are passed through the links are in french (lot of quotes and special chars) and the page treating the link is in UTF8.

I managed to succeed in making all the FB links work, simply by using "mysql_real_escape_string" before I urlencode the content and create a link out of it.

Why "mysql_real_escape_string"?

It was a test because "htmlentities", "htmlspecialchars" and "addslashes" never worked. The obvious downside is that the "mysql_real_escape_string" function has nothing to do there because it's just not related to the database. And indeed, I get an error message telling me that this function "is denied for the user...".

Anyway, I don't plan to use this function but what's weird is that it makes the Facebook sharer link work 100% of the time while "addslashes" doesn't. I thought that addslashes was the pure PHP version of "mysql_real_escape_string" but there is something different about it, otherwise, it should work just the same but it doesn't.

Any pure PHP replacement solution to my problem?

1

There are 1 answers

3
Mark Smit On

The url encode is based on the RFC 1738 standard. The current standard for url encoding has changed to. RFC 3986. The function you want is rawurlencode($string); or a write a function yourself. the characters you want to replace are: '!', '*', "'", "(", ")", ";", ":", "@", "&", "=", "+", "$", ",", "/", "?", "%", "#", "[", "]"