Home > Daily Programming > PHP Knowledge > How to convert HTML entities to ordinary characters in PHP

How to convert HTML entities to ordinary characters in PHP

藏色散人
Release: 2021-01-11 10:50:26
Original
12262 people have browsed it

PHP将HTML实体转换为普通字符,我们可以使用PHP htmlspecialchars_decode()函数将特殊的HTML实体转换为正常字符,如&,<,>(即&,<,>)等HTML实体字符恢复到正常字符。

How to convert HTML entities to ordinary characters in PHP

下面我们就结合代码示例,为大家介绍PHP将HTML实体转换为普通字符的实现方法。

代码示例如下:

<?php
$my_str = "I&#039;m good &amp; &lt;b&gt;&quot;boy &quot;&lt;/b&gt;.";

echo htmlspecialchars_decode($my_str);
echo "<br>";

echo htmlspecialchars_decode($my_str, ENT_QUOTES);
echo "<br>";

echo $my_str;
Copy after login

输出结果如下:

How to convert HTML entities to ordinary characters in PHP

htmlspecialchars_decode函数表示将特殊的 HTML 实体转换回普通字符。

说明 :

string htmlspecialchars_decode ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 ] )
Copy after login

其参数:string表示要解码的字符串,flags表示用下列标记中的一个或多个作为一个位掩码,来指定如何处理引号和使用哪种文档类型。默认为 ENT_COMPAT | ENT_HTML401。

此函数的作用和 htmlspecialchars() 刚好相反。它将特殊的HTML实体转换回普通字符。

被转换的实体有:&, " (没有设置ENT_NOQUOTES 时), ' (设置了 ENT_QUOTES 时), < 以及>。

该htmlspecialchars_decode()函数与htmlspecialchars()将特殊HTML字符转换为HTML实体的函数相反。

本篇文章就是关于PHP将HTML实体转换为普通字符的方法介绍,简单易懂,希望对需要的朋友有所帮助!

The above is the detailed content of How to convert HTML entities to ordinary characters 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