By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,733 Members | 840 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,733 IT Pros & Developers. It's quick & easy.

Reversing htmlspecialchars?

P: 155
I have this when posting to a database:
[PHP]$a = htmlspecialchars($_POST['article']);
$a = addslashes($a);[/PHP]

I have this when displaying the data on a Web page:
[PHP]$article=nl2br(stripslashes(mysql_result($result,$ i,"article")));[/PHP]
The stripslashes works, but the html characters show up, i.e. < >, etc.

htmlspecialchars going into the database is good, but how do I reverse it to display in a Web page?

Jul 24 '07 #1
Share this Question
Share on Google+
3 Replies

P: 155
I found this but how would I implement it?
if ( !function_exists('htmlspecialchars_decode') )
function htmlspecialchars_decode($text)
return strtr($text, array_flip(get_html_translation_table(HTML_SPECIAL CHARS)));

Would I replace $text with my database fields? if so would I need to repeat this code for each field?
Jul 24 '07 #2

Expert 100+
P: 1,584
Its a function. so put it in a global file :
Expand|Select|Wrap|Line Numbers
  1. function htmlspecialchars_decode($text)
  2. {
  3. return strtr($text, array_flip(get_html_translation_table(HTML_SPECIAL CHARS)));
  4. }
and include that file for your php scripts(that you expecting to use the function)

Now call for that function with the Parameters you want to decode.
Expand|Select|Wrap|Line Numbers
  1. $decoded_String = htmlspecialchars_decode($pass_the_string_to_decode)
Jul 24 '07 #3

P: 155
Yes, I overlooked that this was a function for some reason. I have it in my functions page now, thanks.

Jul 24 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.