为什么 PHP 中的这些字符会被转换?
我从数据库中获取了一些信息,例如大量文本,但是在使用 PHP 进行回显时,字符正在发生变化。如何让它显示正常的东西?谢谢。
//makes database connection and queries the db
$row = mysqli_fetch_array($result);
echo $row['text'];
� inside has a question mark
\ before every apostrophe
and it\\\'s
还有更多...
I am getting some information like large amounts of text from the database, but while echoing using PHP, the characters are being changed. How do I get it to display the normal thing? Thanks.
//makes database connection and queries the db
$row = mysqli_fetch_array($result);
echo $row['text'];
� inside has a question mark
\ before every apostrophe
and it\\\'s
and more...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,过度的斜线。这一般是由于
magic_quotes
导致的过度转义,建议将其关闭并对进入数据库的数据使用mysql_real_escape_string
。不过,在关闭它之前,请确保数据已正确转义,因为这可能会在您的站点中引入 SQL 注入漏洞。至于奇怪的角色。您的数据库和网站字符集需要匹配。确保您的网站字符集设置为 utf-8,使用元标记或
header
设置。对于
magic_quotes
来说这是一个艰难的改变,但是一旦完成,您就不必担心使用再次stripslashes
您的数据。因为如果一开始就正确转义了数据库中的数据,那么您不必对来自数据库的数据进行stripslashes
。First up, the excessive slashes. This is generally caused by over escaping which can be due to
magic_quotes
, it is suggested to turn it off and usemysql_real_escape_string
on the data going into the database. Before you turn it off though, make sure that the data is escaped properly, as this could introduce sql injections exploits in your site.As far as the weird character. Your database and website character sets need to match. Make sure your website charset is set to utf-8, either with a meta tag or a
header
setting.Its a hard change for the
magic_quotes
, but once it is done, you do not have to worry about usingstripslashes
on your data again. As you should not have tostripslashes
on data coming out of the database, if it was escaped properly in the first place.