Home > Backend Development > PHP Tutorial > Summary of four security filtering functions in PHP (with code)

Summary of four security filtering functions in PHP (with code)

不言
Release: 2023-04-03 14:54:02
Original
4058 people have browsed it

This article introduces to you a summary of the four security filtering functions in PHP (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Stripslashes() function

The main function of stripslashes() is to delete backslashes

<?php
echo stripslashes("Who\&#39;s Bill Gates?");
?>
Copy after login

Output result:

Who&#39;s Bill Gates?
Copy after login

2. htmlentities() function

htmlentities() converts characters into HTML entities

<?php
$str = "<? W3S?h????>";
echo htmlentities($str);
?>
Copy after login

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

<!DOCTYPE html>
<html>
<body>
<© W3Sçh°°¦§>
</body>
</html>
Copy after login

Browser output of the above code:

<? W3S?h????>
Copy after login

3, htmlspecialchars() function

Convert the predefined characters "<" (less than) and " >" (greater than) converted to HTML entity:

<?php
$str = "This is some <b>bold</b> text.";
echo htmlspecialchars($str);
?>
Copy after login

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

<!DOCTYPE html>
<html>
<body>
This is some <b>bold</b> text.
</body>
</html>
Copy after login

The browser output of the above code:

This is some <b>bold</b> text.
Copy after login

4. strip_tags() function

Strips the HTML tags in the string:

strip_tags() function strips the HTML, XML and PHP in the string Label.

Comments: This function always strips HTML comments. This cannot be changed via the allow parameter.

Note: This function is binary safe.

<?php
echo strip_tags("Hello <b>world!</b>");
?>
Copy after login
Hello world!
Copy after login

Recommended related articles:

Summary of methods for extracting numbers from strings in php (code)

echo statement in PHP5 and What is the difference between print statements

The above is the detailed content of Summary of four security filtering functions in PHP (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template