Search code examples
phphtml-entitieshtmldecode

Decode HTML entities `&lt;` and `&gt;` to `<` and `>` respectively


One of our pages pulls content from a database table using the following code:

<?php echo $project['description']; ?>

What I need is for all &lt; and &gt; instances to be replaced with < and > respectively. Can you help modify the above code to include a preg_replace statement (or str_replace())?


Solution

  • <?php echo htmlspecialchars_decode($project['description']); ?>
    

    Should get you what you need.

    If you are ONLY looking to decode those, though, then:

    <?php echo str_replace("&lt;","<",str_replace("&gt;",">",$project['description'])); ?>
    

    And preg_replace should look like this:

    <?php echo preg_replace(&lt;,"<",preg_replace(&gt;,">",$project['description'])); ?>
    

    I'm pretty sure the & isn't a special character, but if it does cause you issues, put a \ before it.