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?
views:
144answers:
3
+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, "<", "<")
s = Replace(s, ">", ">")
s = Replace(s, "&", "&")
Guffa
2009-09-01 10:25:41
A:
That is just an ascii value of "'", use chr to get it back to a character. Here's the code
$string = "Hello ' Man";
$string = preg_replace('|&#(\d{1,3});|e', 'chr(\1)', $string);
echo $string; # Hello ' Man
roddik
2009-09-01 10:43:59
+4
A:
Please note that mysql_real_escape_string()
does not turn apostrophes '
into '
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(''', ENT_QUOTES);
Josh Davis
2009-09-01 10:47:31