search
HomeBackend DevelopmentPHP TutorialThe definition and usage of htmlentities() function in 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); // 不转换任何引号 
?>

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>

Browser output of the above code:

Bill & &#39;Steve&#39;
Bill & &#39;Steve&#39;
Bill & &#39;Steve&#39;

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 
?>

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>

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
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

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

Video Face Swap

Video Face Swap

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

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools