Htmlspecialchars (), addslashes () _ PHP-php Tutorial

Source: Internet
Author: User
Htmlspecialchars (), addslashes () _ PHP by default, you already know HTML character entities

$ Html ="

";

1. htmlspecialchars ($ html)

Purpose: input the string $ html, convert the reserved characters such as <> in $ html into character entities, and return the converted string.

Application scenarios:

A. to directly display the source code in HTML (equivalent to right-click => view the source code effect), you can use htmlspecialchars () to escape the source code to be output;

B. text filtering: prevents malicious injection on a single table page. for example, enter When htmlspecialchars () is used for escape, <> In this script is converted to a character entity, which is not executed in php.

2. addslashes ($ html)

Purpose: input the string $ html. addslashes () will escape all single quotes and double quotes in $ html.

Application scenarios:

A. Insert a large HTML string into the database


Sample code:

 MH370

";$hsc = htmlspecialchars($html);$ass = addslashes($html);var_dump($html);var_dump($hsc);var_dump($ass);?>

Returned results:

string '

MH370

' (length=26)string '<p id='MH370'>MH370<p>' (length=38)string '

MH370

' (length=28)

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.