views:

144

answers:

3

I saved some data in the database using mysql_real_escape_string() so the single quotes are escaped like this '. It looks ok in the browser, but how can I convert it back to single quote when I save the text in a txt file?

+2  A: 

This is the reason why you should not store encoded text in the database. You should have stored it in it's original format, and encoded it when you display it.

Now you have to check what characters the function does encode, and write string replacements that converts them back, in reverse order.

Pseudo-code example:

s = Replace(s, "'", "'")
s = Replace(s, "&lt;", "<")
s = Replace(s, "&gt;", ">")
s = Replace(s, "&amp;", "&")
Guffa
A: 

That is just an ascii value of "'", use chr to get it back to a character. Here's the code

$string = "Hello &#39; Man";
$string = preg_replace('|&#(\d{1,3});|e', 'chr(\1)', $string);
echo $string; # Hello ' Man
roddik
+4  A: 

Please note that mysql_real_escape_string() does not turn apostrophes ' into &#39; Only HTML-oriented functions do, so you must have calls to htmlentities() somewhere in your script.

As for your question, the function you're looking for is html_entity_decode()

echo html_entity_decode('&#39;', ENT_QUOTES);
Josh Davis