Reversing htmlspecialchars?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DavidPr
    New Member
    • Mar 2007
    • 155

    Reversing htmlspecialchars?

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

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

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

    Thanks.
  • DavidPr
    New Member
    • Mar 2007
    • 155

    #2
    I found this but how would I implement it?
    [PHP]<?php
    if ( !function_exist s('htmlspecialc hars_decode') )
    {
    function htmlspecialchar s_decode($text)
    {
    return strtr($text, array_flip(get_ html_translatio n_table(HTML_SP ECIALCHARS)));
    }
    }
    ?>[/PHP]

    Would I replace $text with my database fields? if so would I need to repeat this code for each field?

    Comment

    • ak1dnar
      Recognized Expert Top Contributor
      • Jan 2007
      • 1584

      #3
      Its a function. so put it in a global file :
      [code=php]
      function htmlspecialchar s_decode($text)
      {
      return strtr($text, array_flip(get_ html_translatio n_table(HTML_SP ECIAL CHARS)));
      }
      [/code]

      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.
      [code=php]
      $decoded_String = htmlspecialchar s_decode($pass_ the_string_to_d ecode)
      [/code]

      Comment

      • DavidPr
        New Member
        • Mar 2007
        • 155

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

        David

        Comment

        Working...