“我是”在PHP中成为“我”? (为什么要添加斜线?)

时间:2012-02-29 23:07:06

标签: php slash

  

可能重复:
  Correctly encode characters in a PHP mail form (“I'm” turns to be “I\'m”)

我正在通过PHP发送电子邮件,文本在引号前面带有斜杠:

I'm变为I\'m

我的PHP:

$text_message = $_POST["mymessage"];       


$message="--PHP-mixed-$bound_text\r\n"      
            ."Content-Type: text/plain; charset=\"utf-8\"\r\n"
 ."Content-Transfer-Encoding: 7bit\r\n\r\n" 
       ."$text_message\r\n\r\n" 
    ."--PHP-mixed-$bound_text\r\n"  
            ."Content-Transfer-Encoding: base64\r\n"
            ."Content-Disposition: attachment; filename=\"$attachment\"\r\n"
."Content-Type: image/jpeg; name=\"$attachment\"\r\n\r\n"
 .chunk_split($file)
        ."\r\n\r\n"
            ."--PHP-mixed-$bound_text--\r\n\r\n";
}

如何在不收到额外斜线的情况下发送? 谢谢。 乌利

3 个答案:

答案 0 :(得分:4)

这是由PHP magic quotes引起的,不推荐使用,但遗憾的是,默认情况下仍然启用。在大多数情况下,您可以通过.htaccess文件或甚至通过webhoster的控制面板禁用该功能。

如果无法做到这一点,最明智的做法是在盲目使用get_magic_quotes_gpc()之前检查是否通过stripslashes()启用了魔术引号。要取消所有$_POST[]个变量,请使用:

if (get_magic_quotes_gpc()) {
    foreach($_POST as $k => $v) {
       $_POST[$k] = stripslashes($v);
    }
}

答案 1 :(得分:2)

你应该检查你的php.ini文件中的magic_quotes,它最有可能, 您可以随时在php中检查此选项并相应地处理字符串

if (get_magic_quotes_gpc()){
   $text_message = stripslashes($_POST["mymessage"]);
}else{
   $text_message = $_POST["mymessage"];
}

而不是使用\r\n,你应该使用PHP_EOL,然后它与所有操作系统兼容:例如,linux不需要\r

答案 2 :(得分:1)

我怀疑这是$_POST["mymessage"]。如果您将$_POST["mymessage"]回显到屏幕,会得到什么?

有些虚拟主机会故意addslashes()收到$_POST$_GET等收到的数据,作为防止SQL注入的基本保护。

如果这样做,您应该能够stripslashes($_POST["mymessage"])