Home > Backend Development > PHP Problem > How to escape single quotes in php

How to escape single quotes in php

coldplay.xixi
Release: 2023-03-08 12:46:02
Original
4526 people have browsed it

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)
Copy after login

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.";?>
Copy after login

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.
Copy after login

Generally used in the following form

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template