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

Dec 24, 2018 pm 04:18 PM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)