Search code examples
phpdatabasehtml-entitieshtmlspecialchars

How to retrieve original text after using htmlspecialchars() and htmlentities()


I have some text that I will be saving to my DB. Text may look something like this: Welcome & This is a test paragraph. When I save this text to my DB after processing it using htmlspecialchars() and htmlentities() in PHP, the sentence will look like this: Welcome & This is a test paragraph.

When I retrieve and display the same text, I want it to be in the original format. How can I do that?

This is the code that I use;

$text= htmlspecialchars(htmlentities($_POST['text'])); 
$text= mysqli_real_escape_string($conn,$text);

Solution

  • There are two problems.

    First, you are double-encoding HTML characters by using both htmlentities and htmlspecialchars. Both of those functions do the same thing, but htmlspecialchars only does it with a subset of characters that have HTML character entity equivalents (the special ones.) So with your example, the ampersand would be encoded twice (since it is a special character), so what you would actually get would be:

    $example = 'Welcome & This is a test paragraph';
    
    $example = htmlentities($example);
    var_dump($example);    // 'Welcome & This is a test paragraph'
    
    $example = htmlspecialchars($example);
    var_dump($example);    // 'Welcome & This is a test paragraph'
    

    Decide which one of those functions you need to use (probably htmlspecialchars will be sufficient) and use only one of them.

    Second, you are using these functions at the wrong time. htmlentities and htmlspecialchars will not do anything to "sanitize" your data for input into your database. (Not saying that's what you're intending, as you haven't mentioned this, but many people do seem to try to do this.) If you want to protect yourself from SQL injection, bind your values to prepared statements. Escaping it as you are currently doing with mysqli_real_escape_string is good, but it isn't really sufficient.

    htmlspecialchars and htmlentities have specific purposes: to convert characters in strings that you are going to output into an HTML document. Just wait to use them until you are ready to do that.