Home Backend Development PHP Tutorial The difference between php htmlspecialchars() and strip_tags functions

The difference between php htmlspecialchars() and strip_tags functions

Jun 08, 2017 pm 02:15 PM

php htmlspecialchars() and strip_tags functions both format HTML codes. Many people think that the functions of htmlentities and htmlspecialchars are the same, but are they really the same? The answer is no, otherwise it would not be the case. There are two functions. This article will take you to understand the difference between php htmlspecialchars() and strip_tags function

First let’s take a look at the usage examples of htmlspecialchars function and strip_tags function:

1

2

3

4

5

6

<?php

$str="<a href=&#39;http://www.php.cn&#39;>php中文网</a>";

echo htmlspecialchars($str);

echo "<br><br>";

echo strip_tags($str);

?>

Copy after login

The code runs the browser output result:

The difference between php htmlspecialchars() and strip_tags functions

#View the page source code, the result is as follows:

1

<a href=&#39;http://www.php.cn&#39;>php中文网&#39;"</a><br/><br/>php中文网&#39;"

Copy after login

It can be seen from the result that htmlspecialchars() and strip_tags The difference is as follows:

Difference 1:

strip_tags function is used to remove HTML tags, while htmlspecialchars does not remove html tags, but only converts tags into HTML instances, so the maximum between the two The difference is that one is to delete the HTML tags, and the other is to convert the html tags into other characters.

Difference 2:

If the tags in the string that need to be removed from the HTML tags are originally wrong, for example, the greater than symbol is missing, an error will be returned when using the strip_tags function, while htmlspecialchars does not There will be errors after converting to HTML entities.

Difference 3:

When preventing XSS attacks, it is generally recommended to use the htmlspecialchars function, because although strip_tags can delete HTML tags, it will not delete " or '. So even if you use strip_tags , you still need to use the htmlspecialchars function to filter out " or '

in form submission or user message board. If you want the original data output to be taken to the browser, then please use the htmlspecialchars function instead of the strip_tags function.

[Related article recommendations]

A brief introduction to the htmlspecialchars, strip_tags, and addslashes functions in php

php Remove string tags strip_tags( ) Detailed explanation of function examples

The above is the detailed content of The difference between php htmlspecialchars() and strip_tags functions. For more information, please follow other related articles on the PHP Chinese website!

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

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

See all articles