php Filter htmlspecialchars of Function Implementation Converts Predefined Characters into HTML Entity Usage Analysis

  • 2021-12-12 08:03:28
  • OfStack

This article illustrates the php filter htmlspecialchars () function to convert predefined characters into HTML entity usage. Share it for your reference, as follows:

This function is very important, especially when dealing with Chinese characters. At the same time, it is often necessary to deal with the data written or read into the database during the development process.

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

1. string: Required. Specifies the string to convert.

2. flags: Optional, specifying how to handle quotation marks, invalid encoding, and which document type to use, such as ENT_COMPAT, ENT_QUOTES, ENT_NOQUOTES

3. character-set: Optional, as the name implies, the default utf-8, of course, supports many encodings, which are not listed here

4. double_encode: Optional, a Boolean value specifying whether to encode an existing HTML entity.

The predefined characters are:

1. & (And) become &
2. "(double quotation marks) become"
3. '(single quotation mark) becomes'
4. < (Less than) Become < > (greater than) become >

So what is an HTML entity?

1. In HTML, some characters are reserved.
2. The less than sign cannot be used in HTML ( < ) and the greater than sign ( > ), because browsers mistake them for tags, and of course there are other entities in HTML
3. If we want to display reserved characters correctly, we must use character entities (character entities) in the HTML source code.
4. To display the less than sign, we must write: < Or < The advantage of using entity names instead of numbers is that names are easy to remember. The downside is that browsers may not support all entity names (entity numbers are well supported).

Example:


<?php
$str = "Apple & 'Orange'";
  echo htmlspecialchars($str, ENT_COMPAT); //  By default, only double quotation marks are encoded 
  // Right-click to view the source code and the result is :Apple & 'Orange'
  echo htmlspecialchars($str, ENT_QUOTES); //  Encoding double quotation marks and single quotation marks 
  // Right-click to view the source code and the result is :Apple & 'Orange'
  echo htmlspecialchars($str, ENT_NOQUOTES); //  Do not encode any quotation marks 
  // Right-click to view the source code and the result is :Apple & 'Orange'
// Extended reading htmlspecialchars_decode()  -   Will be special  HTML  Entity conversion back to ordinary characters 
?>

Supplement: htmlspecialchars_decode ()-Converts special HTML entities back to normal characters

htmlspecialchars_decode() Function to convert 1 predefined HTML entities to characters.

Decoded HTML entity:

& amp; Decode into & (Sum)
& quot; Decoded to "(double quotation marks)
'Decoded' (single quotation marks)
& lt; Decode into < (Less than)
& gt; Decode into > (Greater than)

Example:


<?php
$str = "This is some &lt;b&gt;bold&lt;/b&gt; text.";
echo htmlspecialchars_decode($str);
?>

Output:

This is some < b > bold < /b > text..

PS: Here are some related online tools for your reference:

HTML/XML Escape Character Comparison Table:
http://tools.ofstack.com/table/html_escape

Online HTML escape/reverse meaning tool:
http://tools.ofstack.com/transcoding/html_transcode

For more readers interested in PHP related contents, please check the topics on this site: "Summary of Common Functions and Skills of php", "Summary of Usage of php String (string)", "Encyclopedia of Operation Skills of PHP Array (Array)", "Introduction to Basic Grammar of PHP", "Introduction to Database Operation of php+mysql" and "Summary of Operation Skills of Common Database of php"

I hope this paper is helpful to everyone's PHP programming.


Related articles: