


PHP's function htmlentities() that converts strings into html entities
实例
把一些字符转换为 HTML 实体:
<?php $str = "<© W3CSçh°°¦§>"; echo htmlentities($str); ?>
上面代码的 HTML 输出如下(查看源代码):
<!DOCTYPE html> <html> <body> <© W3CSçh°°¦§> </body> </html>
上面代码的浏览器输出如下:
<© W3CSçh°°¦§>
定义和用法
htmlentities() 函数把字符转换为 HTML 实体。
提示:要把 HTML 实体转换回字符,请使用 html_entity_decode() 函数。
提示:请使用 get_html_translation_table() 函数来返回 htmlentities() 使用的翻译表。
语法
htmlentities(string,flags,character-set,double_encode)
参数 | 描述 |
string | 必需。规定要转换的字符串。 |
flags | 可选。规定如何处理引号、无效的编码以及使用哪种文档类型。 可用的引号类型:
无效的编码:
规定使用的文档类型的附加 flags:
|
character-set | 可选。一个规定了要使用的字符集的字符串。 允许的值:
注释:在 PHP 5.4 之前的版本,无法被识别的字符集将被忽略并由 ISO-8859-1 替代。自 PHP 5.4 起,无法被识别的字符集将被忽略并由 UTF-8 替代。 |
double_encode | 可选。一个规定了是否编码已存在的 HTML 实体的布尔值。
|
技术细节
返回值: | 返回已转换的字符串。 如果 string 包含无效的编码,则返回一个空的字符串,除非设置了 ENT_IGNORE 或者 ENT_SUBSTITUTE 标志。 |
PHP 版本: | 4+ |
更新日志: | 在 PHP 5 中,character-set 参数的默认值改为 UTF-8。 在 PHP 5.4 中,新增了:ENT_SUBSTITUTE、ENT_DISALLOWED、ENT_HTML401、ENT_HTML5、ENT_XML1 和 ENT_XHTML。 在 PHP 5.3 中,新增了 ENT_IGNORE。 在 PHP 5.2.3 中,新增了 double_encode 参数。 在 PHP 4.1 中,新增了 character-set 参数。 |
更多实例
实例 1
把一些字符转换为 HTML 实体:
<?php $str = "Jane & 'Tarzan'"; echo htmlentities($str, ENT_COMPAT); // Will only convert double quotes echo "<br>"; echo htmlentities($str, ENT_QUOTES); // Converts double and single quotes echo "<br>"; echo htmlentities($str, ENT_NOQUOTES); // Does not convert any quotes ?>
上面代码的 HTML 输出如下(查看源代码):
<!DOCTYPE html> <html> <body> Jane & 'Tarzan'<br> Jane & 'Tarzan'<br> Jane & 'Tarzan' </body> </html>
上面代码的浏览器输出如下:
Jane & 'Tarzan' Jane & 'Tarzan' Jane & 'Tarzan'
实例 2
通过使用西欧字符集,把一些字符转换为 HTML 实体:
<?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 ?>
上面代码的 HTML 输出如下(查看源代码):
<!DOCTYPE html> <html> <body> My name is Øyvind Åsane. I'm Norwegian. </body> </html>
上面代码的浏览器输出如下:
My name is Øyvind Åsane. I'm Norwegian.
The above is the detailed content of PHP's function htmlentities() that converts strings into html entities. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide
