search
HomeBackend DevelopmentPHP Tutorialphp htmlentities() function usage examples, htmlentities examples_PHP tutorial

php htmlentities() function usage examples, htmlentities examples

php htmlentities() function converts characters into HTML entities, this article introduces php to coders The basic usage and examples of the htmlentities() function are introduced. Coders in need can refer to it.

Definition and Usage

htmlentities() function converts characters into HTML entities.

Tip: To convert HTML entities back to characters, use the html_entity_decode() function.

Tip: Please use the get_html_translation_table() function to return the translation table used by htmlentities().

Syntax

htmlentities(string,flags,character-set,double_encode)

 

Parameters Description
string Required. Specifies the string to be converted.
flags
参数 描述
string 必需。规定要转换的字符串。
flags

可选。规定如何处理引号、无效的编码以及使用哪种文档类型。

可用的引号类型:

  • ENT_COMPAT - 默认。仅编码双引号。

  • ENT_QUOTES - 编码双引号和单引号。

  • ENT_NOQUOTES - 不编码任何引号。

无效的编码:

  • ENT_IGNORE - 忽略无效的编码,而不是让函数返回一个空的字符串。应尽量避免,因为这可能对安全性有影响。

  • ENT_SUBSTITUTE - 把无效的编码替代成一个指定的带有 Unicode 替代字符 U FFFD(UTF-8)或者 FFFD; 的字符,而不是返回一个空的字符串。

  • ENT_DISALLOWED - 把指定文档类型中的无效代码点替代成 Unicode 替代字符 U FFFD(UTF-8)或者 FFFD;。

规定使用的文档类型的附加 flags:

  • ENT_HTML401 - 默认。作为 HTML 4.01 处理代码。

  • ENT_HTML5 - 作为 HTML 5 处理代码。

  • ENT_XML1 - 作为 XML 1 处理代码。

  • ENT_XHTML - 作为 XHTML 处理代码。

character-set

可选。一个规定了要使用的字符集的字符串。

允许的值:

  • UTF-8 - 默认。ASCII 兼容多字节的 8 位 Unicode

  • ISO-8859-1 - 西欧

  • ISO-8859-15 - 西欧(加入欧元符号 ISO-8859-1 中丢失的法语和芬兰语字母)

  • cp866 - DOS 专用 Cyrillic 字符集

  • cp1251 - Windows 专用 Cyrillic 字符集

  • cp1252 - Windows 专用西欧字符集

  • KOI8-R - 俄语

  • BIG5 - 繁体中文,主要在台湾使用

  • GB2312 - 简体中文,国家标准字符集

  • BIG5-HKSCS - 带香港扩展的 Big5

  • Shift_JIS - 日语

  • EUC-JP - 日语

  • MacRoman - Mac 操作系统使用的字符集

注释:在 PHP 5.4 之前的版本,无法被识别的字符集将被忽略并由 ISO-8859-1 替代。自 PHP 5.4 起,无法被识别的字符集将被忽略并由 UTF-8 替代。

double_encode

可选。布尔值,规定是否编码已存在的 HTML 实体。

  • TRUE - 默认。将对每个实体进行转换。

  • FALSE - 不会对已存在的 HTML 实体进行编码。

Optional. Specifies how to handle quotes, invalid encodings, and which document type to use.

Available quote types:

ENT_COMPAT - Default. Only double quotes are encoded.

  • ENT_QUOTES - encodes double and single quotes.
    返回值:

    返回被转换的字符串。

    如果 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 参数。


  • ENT_NOQUOTES - Do not encode any quotes.

  • Invalid encoding:


    • ENT_IGNORE - ignore invalid encoding instead of letting the function Returns an empty string. This should be avoided as this may have an impact on security.
    • ENT_SUBSTITUTE - Substitute an invalid encoding with the specified character with the Unicode replacement character U FFFD (UTF-8) or FFFD; instead of returning an empty String.
    • ENT_DISALLOWED - Replaces invalid code points in the specified document type with the Unicode replacement characters U FFFD (UTF-8) or FFFD;.
    Additional flags specifying the document type to use:
    • ENT_HTML401 - Default. Code processed as HTML 4.01.
    • ENT_HTML5 - Handles code as HTML 5.
    • ENT_XML1 - As XML 1 processing code.
    • ENT_XHTML - as XHTML processing code.
    character-set Optional. A string specifying the character set to be used. Allowed values:
    • UTF-8 - Default. ASCII compatible multi-byte 8-bit Unicode
    • ISO-8859-1 - Western Europe
    • ISO-8859-15 - Western Europe ( Added French and Finnish letters missing from ISO-8859-1 for Euro symbol)
    • cp866 - DOS-specific Cyrillic character set
    • cp1251 - Cyrillic character set for Windows
    • cp1252 - Western European character set for Windows
    • KOI8-R - Russian li>
    • BIG5 - Traditional Chinese, mainly used in Taiwan
    • GB2312 - Simplified Chinese, national standard character set
    • BIG5-HKSCS - Big5 with Hong Kong extension
    • Shift_JIS - Japanese
    • EUC-JP - Japanese li>
    • MacRoman - Character set used by Mac operating system
    Note: In versions prior to PHP 5.4, unrecognized character sets will be ignored and Replaced by ISO-8859-1. As of PHP 5.4, unrecognized character sets are ignored and replaced by UTF-8.
    double_encode Optional. Boolean value that specifies whether to encode existing HTML entities.
    • TRUE - Default. Each entity will be converted.
    • FALSE - Existing HTML entities will not be encoded.
    Technical detailsIn PHP 5, the default value of the character-set parameter is changed to UTF-8. In PHP 5.4, new: ENT_SUBSTITUTE, ENT_DISALLOWED, ENT_HTML401, ENT_HTML5, ENT_XML1 and ENT_XHTML. In PHP 5.3, ENT_IGNORE was added. In PHP 5.2.3, the double_encode parameter is added. In PHP 4.1, there is a new character-set parameter.
    Return value: Return the converted string. If string contains an invalid encoding, an empty string is returned unless the ENT_IGNORE or ENT_SUBSTITUTE flag is set.
    PHP version: 4
    Update log:
    Statement
    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
    11 Best PHP URL Shortener Scripts (Free and Premium)11 Best PHP URL Shortener Scripts (Free and Premium)Mar 03, 2025 am 10:49 AM

    Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

    Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

    Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

    Build a React App With a Laravel Back End: Part 2, ReactBuild a React App With a Laravel Back End: Part 2, ReactMar 04, 2025 am 09:33 AM

    This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

    Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

    Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

    cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

    The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

    12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

    Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

    Announcement of 2025 PHP Situation SurveyAnnouncement of 2025 PHP Situation SurveyMar 03, 2025 pm 04:20 PM

    The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

    Notifications in LaravelNotifications in LaravelMar 04, 2025 am 09:22 AM

    In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov

    See all articles

    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 Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    Repo: How To Revive Teammates
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    PhpStorm Mac version

    PhpStorm Mac version

    The latest (2018.2.1) professional PHP integrated development tool

    Safe Exam Browser

    Safe Exam Browser

    Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

    SublimeText3 English version

    SublimeText3 English version

    Recommended: Win version, supports code prompts!

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)