Explanation of htmlspecialchars strip_tags and addslashes by PHP

  • 2021-07-06 10:35:01
  • OfStack

htmlspecialchars, strip_tags and addslashes of PHP are common functions in web program development. Today, we will talk about the usage of these functions in detail:

1. Function strip_tags: Remove the labels of HTML and PHP

Note: This function removes any HTML and PHP tag strings contained in the string. If the HTML and PHP labels of the string are already wrong, such as missing greater than symbols, an error will also be returned. This function has the same function as fgetss (). fgetss reads a file from a file and removes the html and php tags.

2. Function htmlspecialchars, which converts special characters into HTML format

Specifically, this function converts the following characters:

& Convert (and) into & amp;
"(Double quotation marks) Turn to & quot;
< Convert (less than) to & lt;
> Convert (greater than) to & gt;

3. Function htmlentities, which converts all characters into HTML strings

Perhaps you are still regretting that htmlspecialchars can only handle 4 html tags, so now don't regret that htmlentities converts all characters. It's not powerful, but it doesn't mean much to me.

4. Functions stripslashes and addslashes are originally a pair, addslashes is a string referenced by backslash, and stripslashes is a string referenced by restoring addslashes.

This function 1 is a necessary step that needs to be processed before database query. This string is preceded by backslashes for database query statements. These characters are single quotation marks ('), double quotation marks ("), backslashes (/), and NUL (NULL characters).


Related articles: