The difference between PHP htmlspecialchars () and Strip_tags functions

Source: Internet
Author: User
PHP htmlspecialchars () and STRIP_TAGS functions are formatted HTML code, a lot of people think htmlentities and htmlspecialchars function is the same, but they are really the same, the answer is negative, Otherwise it will not be the two functions, this article will take you to understand the difference between PHP htmlspecialchars () and Strip_tags functions

Let's take a look at the usage examples of the Htmlspecialchars function and the Strip_tags function:

<?php$str= "<a href= ' http://www.php.cn ' >php Chinese web </a>"; Echo htmlspecialchars ($STR); Echo <br> <br> Echo strip_tags ($STR);? >

The code runs the browser output result:

View the page source code, the results are as follows:

<a href= ' http://www.php.cn ' >php Chinese web ' "</a><br/><br/>php Chinese Network '"

From the results we can see that the difference between htmlspecialchars () and Strip_tags is as follows:

Difference One:

The Strip_tags function uses to remove HTML tags, and htmlspecialchars does not remove the HTML tags, just converts the tags to HTML instances, so the biggest difference between the two is to delete the HTML tags, One is to convert HTML tags to other characters.

Difference Two:

If you need to remove HTML tags in the string inside the original error, such as less than the symbol, the use of the Strip_tags function will return errors, and Htmlspecialchars will not have errors, and then converted to HTML entities.

Difference Three:

It is generally recommended to use the Htmlspecialchars function when preventing XSS attacks, because strip_tags can delete HTML tags, but it does not delete "or". So even if you use strip_tags, you still need to use the Htmlspecialchars function to filter out "or"

In the form submission or user message board, if you want the raw output of the data with the browser, then use the Htmlspecialchars function, do not use the Strip_tags function.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.