htmlspecialchars 输出空白 [英] htmlspecialchars outputting blank

查看:37
本文介绍了htmlspecialchars 输出空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

同时使用 htmlspecialchars 和 htmlentities 会导致诸如 符号甚至单 ' 引号之类的项目产生空白输出.显然,这绝对没用,但是在不使用 html 字符的情况下输出数据会导致 出现这个符号.发生这种情况的任何原因?

Using both htmlspecialchars and htmlentities is causing blank outputs from items such as a symbol and even single ' quotes. Obviously, this is absolutely useless, however outputting the data without using html characters results in this symbol for both �. Any reason why this is occuring?

这是导致问题的代码:

<p>
<?php 
    echo nl2br(htmlspecialchars($aboutarray[0]['about_us'], ENT_COMPAT, "UTF-8")); 
?>
</p>

推荐答案

该字符串未以有效的 UTF-8 编码进行编码.它可能是另一种编码,如 UTF-16,或者它可能只是包含一些不符合任何格式的二进制垃圾.

That string is not encoded in valid UTF-8 encoding. It could be in another encoding like UTF-16 or perhaps it just contains some binary junk that doesn't correspond to any format.

最重要的是,由于您指定了UTF-8"作为 htmlspecialchars() 的编码类型参数,如果字符串不符合UTF-8",它将返回一个空字符串.它在 PHP 手册中说明了这一点.

The bottom line is that, since you specified "UTF-8" as the encoding type parameter of htmlspecialchars(), it will return an empty string if the string does not comply with "UTF-8". It states this in the PHP manual.

一个简单的解决方法是使用替代或忽略标志.更改:

A simple fix is to use the substitute or ignore flag. Change:

htmlspecialchars($aboutarray[0]['about_us'], ENT_COMPAT, "UTF-8")

致:

htmlspecialchars($aboutarray[0]['about_us'], ENT_COMPAT|ENT_SUBSTITUTE, "UTF-8")

或者:

htmlspecialchars($aboutarray[0]['about_us'], ENT_COMPAT|ENT_IGNORE, "UTF-8")

注意:ENT_IGNORE 删除不合规的字节.这可能会导致安全问题.最好真正了解您的字符串以及它的编码方式.纠正问题的根源,而不是使用简单的 ENT_IGNORE 修复.

Note: ENT_IGNORE removes the non-compliant bytes. This could cause a security issue. It's better to truly understand the contents of your string and how it's being encoded. Correct the source of the problem rather than use the simple ENT_IGNORE fix.

你应该问问自己为什么你的字符串不是用 UTF-8 编码的......它应该,但它不是.

You should ask yourself why your string is not encoded in UTF-8... it should be, but it's not.

我刚好也遇到了这个问题;您可以在此处阅读有关为何返回空字符串的详细信息.

I happen to have just encountered this problem as well; you can read details on why an empty string is being returned here.

这篇关于htmlspecialchars 输出空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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