PHP - 替换所有特殊字符以更正显示

时间:2013-07-02 03:03:07

标签: php replace str-replace

我有textarea并将其存储在mysql中。
下面的一些特殊情况,当我到mysql并将其加载到我的区域工作良好

!@#$%^&*()_+-=~<>?{}[]|*-+/:;.,"`

(输入)上传到mysql但不重新加载的角色。我尝试用

重新加载
str_replace("\n", "\\n",$string);

它运作良好

当我到mysql时,一些特殊的下面的字符无效

\'

我尝试在其上方输入双字符,以便上传到mysql但不重新加载

typing '' to mysql ' but i want typing ' to mysql '

我如何以最好的方式使用上面的一些错误字符(向上和重新加载)。感谢


更新我的方式
当我使用

的mysql
addslashes($string)

当我使用

重新加载时
 str_replace("\n", "\\n",addslashes($string))

我的情况完全归功于

2 个答案:

答案 0 :(得分:0)

使用ENT_QUOTES

尝试htmlentities

此外,“\ n”是一个字符。你必须用你的代码替换它。但是你也必须在保存时进行反转换。

答案 1 :(得分:0)

http://www.php.net/manual/en/function.htmlentities.php

echo htmlentities($str, ENT_QUOTES);

可能有效但受mysql转义影响,所以可能是更好的解决方案

http://www.php.net/manual/en/info.configuration.php#ini.magic-quotes-gpc

/.htaccess
php_value magic_quotes_gpc off

将阻止php自动添加斜杠,然后可以使用

将它们添加到sql中
mysql_real_escape_string()

在你的sql语句中

并在使用

读取[for textbox]时删除它们
stripslashes()

http://www.php.net/manual/en/function.stripslashes.php

要在DIV中显示你必须htmlentities(),如上所述