html_entity_decode Implementation of HTML Entity Escape in php

  • 2021-10-16 01:16:17
  • OfStack

Recently encountered a problem, the data contains Chinese quotation marks, the results were escaped to the database, when taking the data with htmlspecialchars_decode to escape the entity back, the results found that it did not take effect, looked at 1 htmlspecialchars_decode only support 5 specified entity conversion, the other [I encountered Chinese quotation marks & ldrquo;]

So html_entity_decode can escape all entities back ~

In addition, if you test it in the browser, you will find that it is escaped back, because the browser automatically processes it. Actually, there is no turn back, you can try it on the command line ~ ~

html_entity_decode: Converts all html entities to original characters

Contrary to htmlentities ()

To be more precise, This function decodes all entities (including all numeric entities): a) must be valid for the selected document type-that is, for XML, this function does not decode named entities that may be defined in some DTD-and b) where characters or characters are in the coded character set associated with the selected encoding and are allowed in the selected document type. All other entities remain as they are.

htmlspecialchars_decode: Converts special HTML entities back to normal characters

This function works just the opposite of htmlspecialchars (). It converts special HTML entities back to ordinary characters.

The converted entities are: & "(when ENT_NOQUOTES is not set)," (when ENT_QUOTES is set), < As well as > .

Therefore, it is impossible to convert the others not included in the above five.

Instances

Convert HTML entities to characters:


<?php
$str = "&lt;&copy; W3CS&ccedil;h&deg;&deg;&brvbar;&sect;&gt;";
echo html_entity_decode($str);
?>

The HTML output of the above code is as follows (see source code):


<!DOCTYPE html>
<html>
<body>
<© W3CSçh ° ° ¦ § >
</body>
</html>

The browser output of the above code is as follows:

< & copy; W3CS & ccedil; h ° & brvbar; § >


Related articles: