为什么php在替换双引号时插入反斜杠 [英] Why does php insert backslash while replacing double quotes

查看:116
本文介绍了为什么php在替换双引号时插入反斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么php在我删除双引号时添加反斜杠。

I'm wondering why php adds a backslash when i remove double quotes.

<input type="text" name="number" id="number" />
<input type="button" name="button" id="button" value="Button" />

假设他们用户输入值5-1 / 2并且我将其传递给处理页面通过jquery的.get方法。

Say they user enters the value 5-1/2" and i'm passing it to a processing page via jquery's .get method.

$('#button').click(function(){

    $.get('determine.php?number='+$('#number').val(),function(data){
     $('#response').html(data);
    });

});

这是我的处理页面。

determine.php

$number = $_GET['number'];

$number = str_replace(array('"', "'"), '', $number);

echo $number;

//echos 5-1/2\

为什么是反斜杠那里?

推荐答案

当你删除斜杠时它不会添加它们,它会在查询字符串参数中自动转义它们 magic_quotes_gpc 指令已启用(和它默认情况下是5.30)。 这是一种安全预防措施,因此可以在数据库查询中安全地使用数据。您可以通过更改php.ini文件中的设置来禁用它们,请参阅 http ://www.php.net/manual/en/security.magicquotes.disabling.php

It doesn't add them when you remove the slash, it automatically escapes them in the query string parameters when the magic_quotes_gpc directive is enabled (and it is, by default pre 5.30). It did this as a security precaution, so that the data could be safely used in a database query. You can disabled them by changing the setting in your php.ini file, see http://www.php.net/manual/en/security.magicquotes.disabling.php.

您还可以使用 stripslashes 将其删除:

You can also use stripslashes to remove them:

$number = str_replace(array('"', "'"), '', stripslashes($number));




使用stripslashes()的一个示例是PHP指令magic_quotes_gpc打开时(默认情况下它是打开的),并且您没有将此数据插入到需要转义的地方(例如数据库)。例如,如果您只是直接从HTML表单输出数据。

An example use of stripslashes() is when the PHP directive magic_quotes_gpc is on (it's on by default), and you aren't inserting this data into a place (such as a database) that requires escaping. For example, if you're simply outputting data straight from an HTML form.

这篇关于为什么php在替换双引号时插入反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆