Parse the difference between PHP addslashes () and addclashes () functions and compare _php techniques

Source: Internet
Author: User

The

PHP addcslashes () function
definition and usage
addcslashes () function adds a backslash before the specified character. The
syntax
addcslashes (string,characters) parameter description
string is required. Specify the string to check.
Characters optional. Specify the character or range of characters affected by addcslashes ().
Hints and comments
notes: Be careful when applying addcslashes () to 0,r,n and T. In PHP, \0,\r,\n and \ t are predefined escape sequences.
Instance
Example 1
in this case, we want to add a backslash to a specific character in the string:
<?php
$str = "Hello, my name is John Adams .";
Echo $str;
Echo addcslashes ($str, ' m ');
Echo addcslashes ($str, ' J ');
?
output:
Hello, my name is John Adams
Hello, \my Na\me is John ada\ms.
Hello, the My name is \john Adams.

and the use of function addslashes ():
PHP addslashes () function
Definition and usage
addslashes () function to add a backslash before the specified predefined character.
These predefined characters are:
• Single quotes (')
• double quotes (")
• backslash (\)
null
syntax
addslashes ( String) parameter to describe the
string required. Specify the string to check.
hint and comment
hint: This function can be used to prepare the appropriate string for the string stored in the database and for the database query statement.
Note: 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.
Example
In this case, we want to add a backslash to the predefined characters in the string:
<?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 John Adams? This isn't safe in a database query.
Who\ ' s John Adams? This is safe in a database query. They all have a corresponding way to remove the backslash they add, respectively: Stripcslashes () and Stripslashes ().

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.