Home Backend Development PHP Tutorial Detailed example of the difference between php stripslashes() function and addslashes() function

Detailed example of the difference between php stripslashes() function and addslashes() function

May 22, 2017 am 11:22 AM

The functions and usage of the addslashes() function and stripslashes() function in PHP were introduced earlier. This article introduces the PHP stripslashes() function and addslashes() function difference.

The difference between them in terms of function names: strip (strip) slashes (slash) add (add) slashes (slash), so one of them is to strip slashes and the other is to add Adding a slash has the opposite effect.

Usage scenarios: In fact, these two functions are mainly used in some data processing that need to convert special characters, such as database operations. When writing to the database, we need to put single quotes like this The special characters are escaped and saved, and when reading,

we need to reverse-escape these escaped special characters in the database.

When we write data to mysql, for example:

mysql_query("update table set `title`='kuhanzhu's blog'");
Copy after login

An error will occur. Like asp, databases are allergic to single quotes. At this time, addslashes has the most face, and it has the same function as asp's replace("'",""","kuhanzhu's blog").

For the sake of security, PHP introduces magic_quotes_gpc = On function, you can directly insert single quotes into the database without any processing. Then for Off, you need to consider the issue of single quotes instead of blindly trusting the operating environment.

When magic_quotes_gpc = When On, the data processed using addslashes() will be saved in the database in the form of \'. If you output it directly at this time, you will find that there are more \'s than you expected, so stripslashes() appears. Can remove \ (different from str_replace("\", "",$Str))

When magic_quotes_gpc = Off, the data processed by addslashes() will be saved in the database in the form of '. , there is no problem with \ as mentioned above. addslashes() can prevent errors in inserting data. If it is output directly at this time, there is no need to use stripslashes() and

addslashes(). stripslashes() is just the opposite, direct memory: addslashes() adds a \, stripslashes() removes a \

So when to use it?

Simply put:

When magic_quotes_gpc = On, the system will automatically handle issues such as single quotes. It doesn’t matter whether you use addslashes() and stripslashes(). However, if addslashes() is used when adding data, stripslashes() must be used when displaying data. )

When magic_quotes_gpc = Off, the system will not handle issues such as single quotes, so you must use addslashes() when inserting data, and you do not need to use stripslashes() 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 it when adding data. addslashes(), when On, stripslashes() cannot be used.

How to determine whether it is On or Off? Aboutget_magic_quotes_gpc(). )Please check this article for the function

Example

The code is as follows:

代码 
//提交数据,或者变量准备:
$Content=addslashes(”这里面是数据,不管有没单引号或者还是变量”);
//插入数据到数据库,代码省略
//开始显示数据
$Content=”从数据库读取的数据”;
if(get_magic_quotes_gpc()){
  $Content=stripslashes($Content); 
}
echo $Content;
Copy after login

[Recommended related articles]:

php addslashes() function and stripslashes() function examples detailed explanation

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

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Does Bitcoin have stocks? Does Bitcoin have equity? Does Bitcoin have stocks? Does Bitcoin have equity? Mar 03, 2025 pm 06:42 PM

Does Bitcoin have stocks? Does Bitcoin have equity?

deepseek What is the difference between r1 and v3 version deepseek What is the difference between r1 and v3 version Feb 19, 2025 pm 03:24 PM

deepseek What is the difference between r1 and v3 version

Summary of FAQs for DeepSeek usage Summary of FAQs for DeepSeek usage Feb 19, 2025 pm 03:45 PM

Summary of FAQs for DeepSeek usage

What is the difference between pre-market and after-market trading? Detailed explanation of the differences between pre-market and after-market trading What is the difference between pre-market and after-market trading? Detailed explanation of the differences between pre-market and after-market trading Mar 03, 2025 pm 11:54 PM

What is the difference between pre-market and after-market trading? Detailed explanation of the differences between pre-market and after-market trading

Why is Bittensor said to be the 'bitcoin' in the AI ​​track? Why is Bittensor said to be the 'bitcoin' in the AI ​​track? Mar 04, 2025 pm 04:06 PM

Why is Bittensor said to be the 'bitcoin' in the AI ​​track?

Is there any difference between South Korean Bitcoin and domestic Bitcoin? Is there any difference between South Korean Bitcoin and domestic Bitcoin? Mar 05, 2025 pm 06:51 PM

Is there any difference between South Korean Bitcoin and domestic Bitcoin?

Pepe bought and sold out in a big way, is MUTM a smarter investment in 2025? Pepe bought and sold out in a big way, is MUTM a smarter investment in 2025? Mar 03, 2025 pm 07:09 PM

Pepe bought and sold out in a big way, is MUTM a smarter investment in 2025?

Vertical proxy: Application scenarios and interpretation of disruptive potential of encryption native proxy Vertical proxy: Application scenarios and interpretation of disruptive potential of encryption native proxy Mar 04, 2025 am 10:21 AM

Vertical proxy: Application scenarios and interpretation of disruptive potential of encryption native proxy

See all articles