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.
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 commentsTips: This function can be used to store Prepare appropriate strings for strings in the database and database query statements. Note: By default, the PHP directivemagic_quotes_gpc is on for all GET , POST and COOKIE data automatically run addslashes().
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's John Adams?";echo $str . " This is not safe in a database query.<br />";echo addslashes($str) . " This is safe in a database query.";?>
Who's John Adams? This is not safe in a database query.Who\'s John Adams? This is safe in a database query.
if(!(get_magic_quotes_gpc())) { $_GET = addslashes($_GET); $_POST = addslashes($_POST); $_COOKIE = addslashes($_COOKIE); }
Related video recommendations:
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!