Home  >  Article  >  Backend Development  >  How to escape single quotes in php

How to escape single quotes in php

coldplay.xixi
coldplay.xixiOriginal
2021-03-04 17:51:364483browse

php method to escape single quotes: Use the [addslashes()] function to add a backslash before the specified predefined character. The syntax is [addslashes(string)]. String is required and is required to be checked. String.

How to escape single quotes in php

The operating environment of this tutorial: Windows 7 system, PHP version 5.6, DELL G3 computer.

php method of escaping single quotes:

PHP addslashes() function

Definition and usage

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

These predefined characters are:

  • Single quote (')

  • Double quote (")

  • Backslash(\)

  • NULL

##Syntax

addslashes(string)

Parameter Description

string Required. Specifies the string to be checked.

Tips and comments

Tips: This function can be used to store Prepare appropriate strings for strings in the database and database query statements.

Note:

By default, the PHP directive

magic_quotes_gpc is on for all GET , POST and COOKIE data automatically run addslashes().

Do not use addslashes() on strings that have been

magic_quotes_gpc escaped, because this will cause double-layer escaping. Encountered In this case, you can use the function get_magic_quotes_gpc() to detect.

Example

In this example, we want to add the Predefined adding backslash:

<?php$str = "Who&#39;s John Adams?";echo $str . " This is not safe in a database query.<br />";echo
addslashes($str)
. " This is safe in a database query.";?>

Output:

Who&#39;s John Adams? This is not safe in a database query.Who\&#39;s John Adams? This is safe in a database query.

Generally used in the following form

if(!(get_magic_quotes_gpc())) {
$_GET = addslashes($_GET);
$_POST = addslashes($_POST);
    $_COOKIE = addslashes($_COOKIE);
}

Related video recommendations:

PHP video tutorial

The above is the detailed content of How to escape single quotes in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn