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

How to escape double quotes in php

藏色散人
藏色散人Original
2021-02-07 09:20:374194browse

php method to escape double quotes: first create a PHP sample file; then add backlash before each double quote through the "addslashes('Shanghai is the "biggest" city in China.');" method Just use a slash to escape the string.

How to escape double quotes in php

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

addslashes — Quote (escape) a string using backslashes; the

addslashes() function returns a string with a backslash added before a predefined character.

The predefined characters are:

单引号(')
双引号(")
反斜杠(\)
NULL

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

Note: By default, PHP automatically runs addslashes() on all GET, POST and COOKIE data. So you should not use addslashes() on already escaped strings, as this will result in double escaping. When encountering this situation, you can use the function get_magic_quotes_gpc() to detect it.

Recommended: "PHP Video Tutorial"

Grammar

addslashes(string)

Parameters

string required. Specifies the string to be escaped.

Add a backslash before each double quote ("):

<?php
$str = addslashes(&#39;Shanghai is the "biggest" city in China.&#39;);
echo($str);
?>

Output:

Shanghai is the \"biggest\" city in China.

The above is the detailed content of How to escape double 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
Previous article:How to call php js methodNext article:How to call php js method