Home > Backend Development > PHP Tutorial > Use html_entity_decode to implement HTML entity escaping in php

Use html_entity_decode to implement HTML entity escaping in php

不言
Release: 2023-03-31 20:44:01
Original
2532 people have browsed it

This article mainly introduces the relevant information about html_entity_decode in php to implement HTML entity escaping. Friends who need it can refer to it

I recently encountered a problem. The data contained Chinese quotation marks, and the results were escaped and stored. Go to the database and use htmlspecialchars_decode to escape the entities back when fetching the data. It turns out that it does not take effect. I looked at htmlspecialchars_decode and only supports 5 specified entity conversions. Others [I encountered Chinese quotation marks "ldrquo;]

So I discovered that html_entity_decode can escape all entities back~

In addition, if you test it in the browser, you will find that it is escaped. This is because the browser automatically It's been dealt with. In fact, there is no conversion back. You can try it on the command line~~

html_entity_decode: Convert all html entities to the original characters

Contrary to htmlentities()

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

htmlspecialchars_decode: Convert special HTML entities back to ordinary characters

The function of this function is exactly the opposite of htmlspecialchars(). It converts special HTML entities back to normal characters.

The entities to be converted are: &, " (when ENT_NOQUOTES is not set), ' (when ENT_QUOTES is set), < and >.

So it is not possible to include the ones not included in the above The other 5 are converted back.

Example

Convert HTML entities to characters:

<?php
$str = "<© W3CSçh°°¦§>";
echo html_entity_decode($str);
?>
Copy after login

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

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

The browser output of the above code is as follows:

<© W3CSçh°°¦§>

The above is the entire content of this article, I hope It will be helpful for everyone’s learning. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

How to use PHP to handle the function of multi-image upload compression

How to solve the problem of curl and soap request service timeout in PHP

The above is the detailed content of Use html_entity_decode to implement HTML entity escaping in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template