Home Backend Development PHP Tutorial The definition and usage of htmlentities() function in php

The definition and usage of htmlentities() function in php

Jun 02, 2018 am 11:29 AM
htmlentities ie php

This article mainly introduces the definition and usage of the htmlentities() function in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

php htmlentities() function converts characters into HTML entities. This article introduces the basic usage and examples of the php htmlentities() function to coders. 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().

Grammar

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

double_encode
Parameters Description
string Required. Specifies the string to be converted.
flags

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.

  • ENT_NOQUOTES - Do not encode any quotes.

Invalid encoding:

  • 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.

  • 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.

  • ENT_DISALLOWED - Replaces invalid code points in the specified document type with the Unicode replacement character 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 - Process code as HTML 5.

  • ENT_XML1 - As XML 1 processing code.

  • ENT_XHTML - Processing code as XHTML.

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 (joining the Euro French and Finnish letters missing from symbols ISO-8859-1)

  • cp866 - DOS-specific Cyrillic character set

  • cp1251 - Windows-specific Cyrillic character set

  • cp1252 - Windows-specific Western European character set

  • KOI8-R - Russian

  • BIG5 - Traditional Chinese, mainly used in Taiwan

  • GB2312 - Simplified Chinese, National Standard Character Set

  • BIG5-HKSCS - With Hong Kong extension Big5

  • Shift_JIS - Japanese

  • EUC-JP - Japanese

  • MacRoman - Mac Operation Character set used by the 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.

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 details

Return value: PHP Version: 4 Change Log:

Example 1

Convert characters to HTML entities:

<?php 
$str = "Bill & &#39;Steve&#39;"; 
echo htmlentities($str, ENT_COMPAT); // 只转换双引号 
echo "<br>"; 
echo htmlentities($str, ENT_QUOTES); // 转换双引号和单引号 
echo "<br>"; 
echo htmlentities($str, ENT_NOQUOTES); // 不转换任何引号 
?>
Copy after login

The HTML output of the above code is as follows (view source code):

<!DOCTYPE html> 
<html> 
<body> 
Bill & &#39;Steve&#39;<br> 
Bill & &#39;Tarzan&#39;<br> 
Bill & &#39;Steve&#39;
</body> 
</html>
Copy after login

Browser output of the above code:

Bill & &#39;Steve&#39;
Bill & &#39;Steve&#39;
Bill & &#39;Steve&#39;
Copy after login

Example 2

Convert some characters into HTML entities by using the Western European character set:

<?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 
?>
Copy after login

The HTML output of the above code is as follows (view source code):

<!DOCTYPE html> 
<html> 
<body> 
My name is Øyvind Åsane. I&#39;m Norwegian. 
</body> 
</html>
Copy after login

The browser output of the above code:

My name is ?yvind ?sane. I'm Norwegian.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

PHP MariaDB database operation basic skills memo summary

phpAchieving manipulation of mysqli database Method

PHP Method to access Alipay’s instant payment function

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.

In In 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 is added.

In PHP 5.2.3, the

double_encode parameter was added.

In PHP 4.1, the

character-set parameter was added.

The above is the detailed content of The definition and usage of htmlentities() function 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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles