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)

不言
不言Original
2018-08-03 13:59:464020browse

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?");
?>

Output result:

Who&#39;s Bill Gates?

2. htmlentities() function

htmlentities() converts characters into HTML entities

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

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

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

Browser output of the above code:

<? W3S?h????>

3, htmlspecialchars() function

Convert the predefined characters "448aca1b32fd4c68ec1ee71fe5da5ba6" (greater than) converted to HTML entity:

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

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>

The browser output of the above code:

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

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>");
?>
Hello world!

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!

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