Home  >  Article  >  Backend Development  >  stripslashes() function and addslashes() function in PHP

stripslashes() function and addslashes() function in PHP

angryTom
angryTomforward
2019-10-14 18:31:542585browse

This article will introduce you to the stripslashes() function and addslashes() function in PHP.

1. stripslashes()——Delete backslashes

Definition and usage

The stripslashes() function deletes the backslashes added by the addslashes() function Backslash.

Tip: This function can be used to clean data retrieved from the database or from an HTML form.

<?php
 
echo stripslashes("Who\&#39;s Bill Gates?");

2. addslashes()——Add a backslash before each double quote (")

Definition and usage

## The #addslashes() function returns a string with a backslash added before the predefined characters.

The predefined characters are:

single quote (')

double quote ( ")

Backslash (\)

NULL

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

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.

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

3. Notes

1. When magic_quotes_gpc = on, the system will automatically handle issues such as single quotes. Do you need to use addslashes() or stripslashes? () doesn’t matter, but if addslashes() is used when adding data, stripslashes()

2 is necessary when displaying data. When magic_quotes_gpc = off, the system will not handle issues such as single quotes, so insert Addslashes() must be used when data is displayed, but stripslashes() is not required when displaying data.

Now that we have the analysis, what should we do when doing the program? According to the above two situations, we can get:

Regardless of whether magic_quotes_gpc is On or Off, we use addslashes() when adding data. When On, stripslashes() must be used, and when Off, stripslashes() cannot be used. .

How to judge on or off? Use get_magic_quotes_gpc().

For more PHP related knowledge, please visit

PHP Chinese website!

The above is the detailed content of stripslashes() function and addslashes() function in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:www.whmblog.cn. If there is any infringement, please contact admin@php.cn delete