<blockquote><p>So konvertieren Sie Zeichen in PHP: 1. Verwenden Sie die Funktion htmlentities(), um Zeichen in HTML-Entitäten zu konvertieren. 2. Verwenden Sie die Funktion htmlspecialchars(), um einige vordefinierte Zeichen ("&", "< ", " zu konvertieren. >", etc.) werden in HTML-Entitäten umgewandelt. <!--”、“--></p></blockquote>
<p><img src="https://img.php.cn/upload/article/000/000/024/6229b74393967375.jpg" alt="So konvertieren Sie Zeichen in PHP in Entitäten" ></p>
<p>Die Betriebsumgebung dieses Tutorials: Windows 7-System, PHP-Version 7.1, DELL G3-Computer </p>
<p><span style="font-size: 18px;"><strong>php konvertiert Zeichen in Entitäten </strong></span></p>
<p><strong> 1. Verwenden Sie die Funktion htmlentities() </strong></p>
<p> Die Funktion htmlentities( ) wandelt Zeichen in HTML-Entitäten um. </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">Nach dem Login kopieren</div></div><table class="reference notranslate"><tbody><tr class="firstRow"><th width="20%">Parameter</th><th width="80%">Beschreibung</th></tr><tr><td><em>Zeichenfolge</em></td><td>Erforderlich. Gibt die zu konvertierende Zeichenfolge an. </td></tr><tr><td><em>Flaggen</em></td><td>optional. Gibt an, wie mit Anführungszeichen und ungültigen Kodierungen umgegangen wird und welcher Dokumenttyp verwendet werden soll. <p>Verfügbare Angebotstypen: </p><ul><li>ENT_COMPAT – Standard. Es werden nur doppelte Anführungszeichen codiert. </li><li>ENT_QUOTES – Kodieren Sie doppelte und einfache Anführungszeichen. </li><li>ENT_NOQUOTES – Keine Anführungszeichen kodieren. </li></ul><p>Ungültige Kodierung: </p><ul><li>ENT_IGNORE – Ungültige Kodierungen ignorieren, anstatt dass die Funktion eine leere Zeichenfolge zurückgibt. Dies sollte vermieden werden, da dies Auswirkungen auf die Sicherheit haben kann. </li><li>ENT_SUBSTITUTE – Ersetzen Sie eine ungültige Codierung durch das angegebene Zeichen durch das Unicode-Ersatzzeichen U+FFFD (UTF-8) oder FFFD;, anstatt eine leere Zeichenfolge zurückzugeben. </li><li>ENT_DISALLOWED – <span lang="no-bok">Ersetzen Sie ungültige Codepunkte im angegebenen Dokumenttyp durch die Unicode-Ersatzzeichen U+FFFD (UTF-8) oder FFFD;. </span></li></ul><p>Zusätzliche Flags, die den zu verwendenden Dokumenttyp angeben: </p><ul><li>ENT_HTML401 – Standard. Code verarbeitet als HTML 4.01. </li><li>ENT_HTML5 – Behandelt Code als HTML 5. </li><li>ENT_XML1 – Als XML 1-Verarbeitungscode. </li><li>ENT_XHTML – als XHTML-Verarbeitungscode. </li></ul></td></tr><tr><td><em>Zeichensatz</em></td><td>optional. Eine Zeichenfolge, die den zu verwendenden Zeichensatz angibt. <p>Zulässige Werte: </p><ul><li>UTF-8 – Standard. ASCII-kompatibler Multibyte-8-Bit-Unicode </li><li>ISO-8859-1 – Westeuropa </li><li>ISO-8859-15 – Westeuropa (Eurozeichen hinzugefügt + französische und finnische Buchstaben fehlen in ISO-8859-1) </li><li>cp866 – DOS- spezifischer kyrillischer Zeichensatz </li><li>cp1251 – Windows-spezifischer kyrillischer Zeichensatz </li><li>cp1252 – Windows-spezifischer westeuropäischer Zeichensatz </li><li>KOI8-R – Russisch </li><li>BIG5 – Traditionelles Chinesisch, hauptsächlich in Taiwan verwendet </li><li>GB2312 – Vereinfachtes Chinesisch, nationaler Standard Zeichensatz </li><li>BIG5-HKSCS – Big5 mit Hongkong-Erweiterung </li><li>Shift_JIS – Japanisch </li><li>EUC-JP – Japanisch </li><li>MacRoman – Zeichensatz, der vom Mac-Betriebssystem verwendet wird </li></ul><p><strong>Hinweis: </strong>In Versionen vor PHP 5.4 , Nicht erkannte Zeichensätze werden ignoriert und durch ISO-8859-1 ersetzt. Ab PHP 5.4 werden nicht erkannte Zeichensätze ignoriert und durch UTF-8 ersetzt. </p></td></tr><tr><td><em>double_encode</em></td><td>Optional. Ein boolescher Wert, der angibt, ob vorhandene HTML-Entitäten codiert werden sollen. <ul><li>TRUE – Standard. Jede Entität wird konvertiert. </li><li>FALSE – Vorhandene HTML-Entitäten werden nicht codiert. </li></ul></td></tr></tbody></table><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&#39;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">Nach dem Login kopieren</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 &Oslash;yvind &Aring;sane. I&#039;m Norwegian.
</body>
</html></pre><div class="contentsignin">Nach dem Login kopieren</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&#39;m Norwegian.</pre><div class="contentsignin">Nach dem Login kopieren</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">Nach dem Login kopieren</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 & &#39;Tarzan&#39;";
echo htmlspecialchars($str, ENT_COMPAT); // 默认,仅编码双引号
echo "<br>";
echo htmlspecialchars($str, ENT_QUOTES); // 编码双引号和单引号
echo "<br>";
echo htmlspecialchars($str, ENT_NOQUOTES); // 不编码任何引号
?></pre><div class="contentsignin">Nach dem Login kopieren</div></div><p>输出结果:</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">Jane & &#39;Tarzan&#39;
Jane & &#39;Tarzan&#39;
Jane & &#39;Tarzan&#39;</pre><div class="contentsignin">Nach dem Login kopieren</div></div><p>推荐学习:《<a href="https://www.php.cn/course/list/29/type/2.html" target="_blank">PHP视频教程</a>》</p>
Das obige ist der detaillierte Inhalt vonSo konvertieren Sie Zeichen in PHP in Entitäten. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!