Home > Backend Development > PHP Problem > How to convert characters into entities in php

How to convert characters into entities in php

青灯夜游
Release: 2023-03-15 15:26:01
Original
3676 people have browsed it
<blockquote><p>php method to convert characters into entities: 1. Use the htmlentities() function to convert characters into HTML entities; 2. Use the htmlspecialchars() function to convert some predefined characters ("&" , "<", ">", etc.) into HTML entities. <!--”、“--></p></blockquote> <p><img src="https://img.php.cn/upload/article/000/000/024/6229b74393967375.jpg" alt="How to convert characters into entities in php" ></p> <p>The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer</p> <p><span style="font-size: 18px;"><strong>php Convert characters into entities</strong></span></p> <p><strong>1. Use the htmlentities() function</strong></p> <p>htmlentities() function to convert characters into HTML entities. </p> <p>Syntax:<br></p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">htmlentities(string,flags,character-set,double_encode)</pre><div class="contentsignin">Copy after login</div></div><table class="reference notranslate"><tbody><tr class="firstRow"><th width="20%">Parameters</th><th width="80%">Description</th></tr><tr><td><em> string</em></td><td>Required. Specifies the string to be converted. </td></tr><tr><td><em>flags</em></td><td> Optional. Specifies how to handle quotes, invalid encodings, and which document type to use. <p>Available quote types: </p><ul><li>ENT_COMPAT - Default. Only double quotes are encoded. </li><li>ENT_QUOTES - Encodes double and single quotes. </li><li>ENT_NOQUOTES - Do not encode any quotes. </li></ul><p>Invalid encoding: </p><ul><li>ENT_IGNORE - Ignore invalid encodings instead of having the function return an empty string. This should be avoided as this may have an impact on security. </li><li>ENT_SUBSTITUTE - Substitutes an invalid encoding with the specified character with the Unicode substitution character U FFFD (UTF-8) or &#FFFD; instead of returning an empty string. </li><li>ENT_DISALLOWED - <span lang="no-bok">Replace invalid code points in the specified document type with the Unicode replacement character U FFFD (UTF-8) or &#FFFD;. </span></li></ul><p>Additional flags specifying the document type to use: </p><ul><li>ENT_HTML401 - Default. Code processed as HTML 4.01. </li><li>ENT_HTML5 - Process code as HTML 5. </li><li>ENT_XML1 - Code processed as XML 1. </li><li>ENT_XHTML - Processing code as XHTML. </li></ul></td></tr><tr><td><em>character-set</em></td><td> Optional. A string specifying the character set to be used. <p> Allowed values: </p><ul><li>UTF-8 - Default. ASCII compatible multi-byte 8-bit Unicode</li><li>ISO-8859-1 - Western Europe</li><li>ISO-8859-15 - Western Europe (adds French and Finnish euro symbols missing from ISO-8859-1 Chinese letters) </li><li>cp866 - DOS-specific Cyrillic character set </li><li>cp1251 - Windows-specific Cyrillic character set </li><li>cp1252 - Windows-specific Western European character set </li><li>KOI8- R - Russian</li><li>BIG5 - Traditional Chinese, mainly used in Taiwan</li><li>GB2312 - Simplified Chinese, National Standard Character Set</li><li>BIG5-HKSCS - Big5## with Hong Kong extension </li>#Shift_JIS - Japanese <li></li>EUC-JP - Japanese <li></li>MacRoman - Character set used by Mac operating systems <li></li></ul><p>Comments: <strong> In versions prior to PHP 5.4, unrecognized character sets were ignored and replaced by ISO-8859-1. As of PHP 5.4, unrecognized character sets are ignored and replaced by UTF-8. </strong></p></td></tr><tr><td>double_encode<em></em></td> Optional. A Boolean value that specifies whether to encode existing HTML entities. <td><ul>TRUE - Default. Each entity will be converted. <li></li>FALSE - Existing HTML entities will not be encoded. <li></li></ul></td></tr></tbody><p>示例:通过使用西欧字符集,把一些字符转换为 HTML 实体:</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><?php $str = "My name is Øyvind Åsane. I'm Norwegian."; echo htmlentities($str, ENT_QUOTES, "ISO-8859-1"); // Will only convert double quotes (not single quotes), and uses the character-set Western European ?></pre><div class="contentsignin">Copy after login</div></div><p>上面代码的 HTML 输出如下(查看源代码):</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><!DOCTYPE html> <html> <body> My name is Øyvind Åsane. I'm Norwegian. </body> </html></pre><div class="contentsignin">Copy after login</div></div><p>上面代码的浏览器输出如下:</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">My name is Øyvind Åsane. I'm Norwegian.</pre><div class="contentsignin">Copy after login</div></div><p><strong>2、使用htmlspecialchars()函数</strong></p><p>htmlspecialchars() 函数把一些预定义的字符转换为 HTML 实体。</p><p>预定义的字符是:</p><ul style="list-style-type: disc;"><li><p><code>& </code>(和号)成为 <code>&</code></p></li><li><p><code>"</code> (双引号)成为 <code>"</code></p></li><li><p><code>'</code> (单引号)成为 <code>'</code></p></li><li><p><code><</code> (小于)成为 <code><</code></p></li><li><p><code>></code> (大于)成为 <code>></code></p></li></ul><p>语法:</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">htmlspecialchars(string,flags,character-set,double_encode)</pre><div class="contentsignin">Copy after login</div></div><table class="reference"><tbody><tr class="firstRow"><th width="20%">参数</th><th width="80%">描述</th></tr><tr><td><em>string</em></td><td>必需。规定要转换的字符串。</td></tr><tr><td><em>flags</em></td><td>可选。规定如何处理引号、无效的编码以及使用哪种文档类型。<p>可用的引号类型:</p><ul><li>ENT_COMPAT - 默认。仅编码双引号。</li><li>ENT_QUOTES - 编码双引号和单引号。</li><li>ENT_NOQUOTES - 不编码任何引号。</li></ul><p>无效的编码:</p><ul><li>ENT_IGNORE - 忽略无效的编码,而不是让函数返回一个空的字符串。应尽量避免,因为这可能对安全性有影响。</li><li>ENT_SUBSTITUTE - 把无效的编码替代成一个指定的带有 Unicode 替代字符 U+FFFD(UTF-8)或者 &#FFFD; 的字符,而不是返回一个空的字符串。</li><li>ENT_DISALLOWED - <span lang="no-bok">把指定文档类型中的无效代码点替代成 Unicode 替代字符 U+FFFD(UTF-8)或者 &#FFFD;。</span></li></ul><p>规定使用的文档类型的附加 flags:</p><ul><li>ENT_HTML401 - 默认。作为 HTML 4.01 处理代码。</li><li>ENT_HTML5 - 作为 HTML 5 处理代码。</li><li>ENT_XML1 - 作为 XML 1 处理代码。</li><li>ENT_XHTML - 作为 XHTML 处理代码。</li></ul></td></tr><tr><td><em>character-set</em></td><td>可选。一个规定了要使用的字符集的字符串。<p>允许的值:</p><ul><li>UTF-8 - 默认。ASCII 兼容多字节的 8 位 Unicode</li><li>ISO-8859-1 - 西欧</li><li>ISO-8859-15 - 西欧(加入欧元符号 + ISO-8859-1 中丢失的法语和芬兰语字母)</li><li>cp866 - DOS 专用 Cyrillic 字符集</li><li>cp1251 - Windows 专用 Cyrillic 字符集</li><li>cp1252 - Windows 专用西欧字符集</li><li>KOI8-R - 俄语</li><li>BIG5 - 繁体中文,主要在台湾使用</li><li>GB2312 - 简体中文,国家标准字符集</li><li>BIG5-HKSCS - 带香港扩展的 Big5</li><li>Shift_JIS - 日语</li><li>EUC-JP - 日语</li><li>MacRoman - Mac 操作系统使用的字符集</li></ul><p><strong>注释:</strong>在 PHP 5.4 之前的版本,无法被识别的字符集将被忽略并由 ISO-8859-1 替代。自 PHP 5.4 起,无法被识别的字符集将被忽略并由 UTF-8 替代。</p></td></tr><tr><td><em>double_encode</em></td><td>可选。一个规定了是否编码已存在的 HTML 实体的布尔值。<ul><li>TRUE - 默认。将对每个实体进行转换。</li><li>FALSE - 不会对已存在的 HTML 实体进行编码。</li></ul></td></tr></tbody></table><p>返回值::</p><ul style="list-style-type: disc;"><li><p>返回已转换的字符串。如果 string 包含无效的编码,则返回一个空的字符串,除非设置了 ENT_IGNORE 或者 ENT_SUBSTITUTE 标志。 </p></li></ul><p>示例:把一些预定义的字符转换为 HTML 实体</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><?php $str = "Jane & 'Tarzan'"; echo htmlspecialchars($str, ENT_COMPAT); // 默认,仅编码双引号 echo "<br>"; echo htmlspecialchars($str, ENT_QUOTES); // 编码双引号和单引号 echo "<br>"; echo htmlspecialchars($str, ENT_NOQUOTES); // 不编码任何引号 ?></pre><div class="contentsignin">Copy after login</div></div><p>输出结果:</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">Jane & 'Tarzan' Jane & 'Tarzan' Jane & 'Tarzan'</pre><div class="contentsignin">Copy after login</div></div><p>推荐学习:《<a href="https://www.php.cn/course/list/29/type/2.html" target="_blank">PHP视频教程</a>》</p>

The above is the detailed content of How to convert characters into entities in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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