PHP stripslashes and addslashes usage differences _php Tutorials

Source: Internet
Author: User
In PHP we often use stripslashes and addslashes, let me explain in detail the stripslashes and addslashes use of the difference between them.

Addslashes

The Addslashes () function adds a backslash before the specified predefined character.

These predefined characters are:

• Single quotation mark (')
• Double quotation marks (")
• back slash ()
NULL

In this case, we're going to add a backslash to the predefined characters in the string:

Note: 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.

copy code

/**
* Determine whether to process
*
with Addslashes () param String $STR
*
*/

Function str_addslashes ($str, $db _type= ' MySQL ') {
if (GET_MAGIC_QUOTES_GPC ()) {
Switch ($db _type) {
Case "access":
$str = stripslashes ($STR);
$str = Str_replace ("'", "'", $str);
break;
}
}else {
switch ($db _type) {
case "MySQL":
$str = addslashes ($STR);
Break
Case "Access":
$str = Str_replace ("'", "'", $str);
Break
}
}
return $str;
}

Custom Function Str_addslashes Description: If we do not know whether MAGIC_QUOTES_GPC is open during the commit process, we can take this way to handle the MySQL database when on, but the Access database is still to be removed first, Then replace the single quotation mark with double quotation marks. When off the MySQL database plus

Stripslashes ()

The Stripslashes () function deletes a backslash added by the addslashes () function.

Cases

Code

The code is as follows Copy Code

Submit data, or variable preparation:

$Content =addslashes ("This is data, whether there is a single quote or a variable");

Inserting data into the database, omitting the code

Start displaying data

$Content = "Data read from the database";

if (GET_MAGIC_QUOTES_GPC ()) {
$Content =stripslashes ($Content);
}

Echo $Content;

Summary of differences

When MAGIC_QUOTES_GPC = ON, the data processed using addslashes () will be saved in the database as ' form ', and if it is directly output at this time, it will find more than the content you expect, so Stripslashes () has appeared, It can be removed (different from Str_replace ("", "", $Str)).

When MAGIC_QUOTES_GPC = off, using the addslashes () processed data in the database will be ' form ', there is no problem mentioned above, addslashes () play the role of inserting data without error, if the direct output at this time, the data is normal. No need to use stripslashes ().

Addslashes () and stripslashes () are just the opposite, Direct Memory: addslashes () Add a, stripslashes () go to a

http://www.bkjia.com/PHPjc/628898.html www.bkjia.com true http://www.bkjia.com/PHPjc/628898.html techarticle in PHP We often use stripslashes and addslashes, let me explain in detail the stripslashes and addslashes use of the difference between them. Addslashes addslashes () 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.