Home >Backend Development >PHP Problem >php addslashes escape method

php addslashes escape method

藏色散人
藏色散人Original
2021-02-28 09:54:262510browse

php addslashes escape method: first create a PHP sample file; then define a string; finally add backslashes to the predefined characters in the string through "addslashes($str)".

php addslashes escape method

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

addslashes definition and usage

addslashes() function returns a string with backslashes added before predefined characters.

The predefined characters are:

Single quote (')

Double quote (")

Backslash (\ )

NULL

Tip: This function can be used to prepare strings for strings stored in the database and database query statements.

Note: By default, PHP GET, POST and COOKIE data automatically runs addslashes(). So you should not use addslashes() on escaped strings, as this will cause double-level escaping. When encountering this situation, you can use the function get_magic_quotes_gpc() Perform detection.

Syntax

addslashes(string)

Parameters

string Required. Specifies the string to be escaped.

Technical details

Return Value: Returns the escaped string.

PHP Version: 4

Example

Add backslash to predefined characters in a string Bar:

<?php
$str = "Who&#39;s Bill Gates?";
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 Bill Gates? This is not safe in a database query.
Who\&#39;s Bill Gates? This is safe in a database query.

[Recommended learning: "PHP Video Tutorial"]

The above is the detailed content of php addslashes escape method. 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