PHP backslash function addslashes () and stripslashes () tutorial

Source: Internet
Author: User
Tags html form

PHP Backslash handler function

Addslashes (): Adds a backslash to some predefined characters in the input string so that the processing is required for database query statements, and so on. These predefined characters are: Single quotes ('), double quotes ("), backslash (\), NULL.

Stripslashes (): Deletes the backslash that was added by the addslashes () function. This function is used to clean up data retrieved from a database or HTML form. (if two consecutive backslash, then remove one, keep one, if only a backslash, directly removed.) )

By default, the PHP directive MAGIC_QUOTES_GPC to on and automatically runs Addslashes () for all get, POST, and COOKIE data. Do not use Addslashes () on strings that have been escaped by MAGIC_QUOTES_GPC, because this can result in a double escape. You can use the function GET_MAGIC_QUOTES_GPC () for instrumentation when this situation is encountered. Cases:

if (GET_MAGIC_QUOTES_GPC ()) {
Code .....
}

Addslashes () Example:

<?php
$str = "Who ' s John Adams?";
Echo$str. " This isn't safe in a database query.<br/> ";
Echoaddslashes ($STR). " This is safe in a database query.
?>

Output results:

Who ' s John Adams? This isn't safe in a database query.
Who\ ' s John Adams? This is safe in a database query.

Stripslashes () Example:

<?php
Echostripslashes ("who\ ' s John Adams)";
?>

Output results:

Who ' s John Adams?
--> --> -->
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.