php回显来自mysql数据库中代码的富文本

I am wondering how to echo data stored in a database as rich text. I can put the data in the database, but it stores it as code. I want it to echo as formatted text using PHP. For example, if one of my users inputs the chemical formula CO2 with the 2 subscripted, it will be stored in the MySQL database as the code:

     CO<sub>2</sub>

I don't want this code to be echoed. Instead I want the formatted text "CO2" to be echoed. Is this possible and if so how? I would like all the rich text markup to be echoed instead as actual formatted text rather than code.

Before insert in your database use htmlspecialchars($string);

Example:

$string = "CO<sub>2</sub>";
$stringToInsert = htmlspecialchars($string);

After insert the $stringToInsert in your database, you can print into your page like this:

$decodedString = htmlspecialchars_decode($stringToInsert);
echo $decodedString;

I preffer using htmlspecialchars instead of htmlentities for this case.

It turns out that the PHP script is echoing the text correctly, but that my user front-end (an HTML5/javascript application) is then taking the echoed formatted text and writing it as code. So, in short, the echoing is working in php. Now I just need to figure out how to cause my front-end application to show the text as rich text, not code.