简体   繁体   English

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

[英]PHP - replace all special characters to correct display

I have textarea and i store it in mysql. 我有textarea并将其存储在mysql中。
some special below when i up to mysql and load it to my area working well 当我使用mysql并将其加载到工作良好的区域时,下面有一些特殊的地方

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

(enter) character done with upload to mysql but not to reload. (输入)字符已完成上传到mysql,但未重新加载。 I try to reload with 我尝试重新加载

str_replace("\n", "\\n",$string);

and it working well 而且效果很好

When i up to mysql some special below characters not working 当我使用mysql时,以下一些特殊字符不起作用

\'

I try typing double character above it work for upload to mysql but not to reload 我尝试在其上面输入双字符以将其上传到mysql,但不重新加载

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

How can i work with some error charactor above (up and reload) with best way. 我如何以最佳方式使用上面的某些错误特征(启动和重新加载)。 thanks 谢谢


Update my way 更新我的方式
When i up to mysql i using 当我使用mysql时

addslashes($string)

and when i reload i using 当我重新加载我使用

 str_replace("\n", "\\n",addslashes($string))

It's done with my case thanks all 我的情况已经完成了,谢谢大家

Try htmlentities with ENT_QUOTES 使用ENT_QUOTES尝试htmlentities

Also, "\\n" is a one char. 另外,“ \\ n”是一个字符。 You have to replace it with your code. 您必须将其替换为您的代码。 But also you must do back convert when saving. 但是,保存时还必须进行反向转换。

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

echo htmlentities($str, ENT_QUOTES);

might work but subject to mysql escaping, so a possibly better solution 可能可以工作,但是要经受mysql的转义,所以可能是更好的解决方案

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

/.htaccess
php_value magic_quotes_gpc off

will prevent php auto-adding the slashes, you can then add them in sql with 将阻止php自动添加斜杠,然后可以使用

mysql_real_escape_string()

in your sql statement 在你的sql语句中

and removing them upon read [for textbox] with 并在阅读[for textbox]时将其删除

stripslashes()

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

to display in a DIV you'd have to htmlentities() as explained above 要显示在DIV中,您必须如上所述htmlentities()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM