PHP usage of addslashes () and stripslashes () for backslash handling functions

Source: Internet
Author: User

Http://yangjunwei.com/a/589.html

PHP's own library functions Addslashes () and Stripslashes () are string-processing class functions that are the opposite:

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

Stripslashes (): Removes the backslash added by the addslashes () function. This function cleans up data retrieved from a database or HTML form. (if two consecutive backslashes, remove one, keep one; if there is only one backslash, remove it directly.) )

PS: By default, PHP instruction MAGIC_QUOTES_GPC is on, and automatically runs Addslashes () for all GET, POST, and COOKIE data. Do not use Addslashes () for strings that have been MAGIC_QUOTES_GPC escaped, because this results in double-layer escaping. You can use the function GET_MAGIC_QUOTES_GPC () to detect this situation. 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/>";
echo addslashes ($STR). "This is safe in a database query.";
?>

Output:

Who ' s John Adams? This is the not safe in a database query.
Who\ ' s John Adams? This is safe in a database query.

Stripslashes () Example:

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

Output:

Who ' s John Adams?

PHP usage of addslashes () and stripslashes () for backslash handling functions

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.