htmlentities 返回空字符串

发布于 2024-12-20 02:59:58 字数 351 浏览 5 评论 0原文

以下代码输出一个空字符串。原因是 $text 中的“ó”,但为什么呢?那么utf-8编码的是哪些字符呢?

使用iso-8859-1时问题解决了,但我需要使用utf-8,那么我做错了什么?

<!doctype html>
<head>
  <meta charset="utf-8">
</head>

<body>
<?
$text = 'Hola ó Hola';
$text = htmlentities($text,ENT_QUOTES,'utf-8');
echo $text;
?>
</body>
</html>

The following code outputs an empty string. The cause is the "ó" in $text, but why? What characters does utf-8 encode then?

The problem is solved when using iso-8859-1, but I need to use utf-8, so what am I doing wrong?

<!doctype html>
<head>
  <meta charset="utf-8">
</head>

<body>
<?
$text = 'Hola ó Hola';
$text = htmlentities($text,ENT_QUOTES,'utf-8');
echo $text;
?>
</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

愛放△進行李 2024-12-27 02:59:58

我遇到了类似的问题,并使用标志 ENT_SUBSTITUTE 来防止空字符串。它仍然没有编码,而且我不能依赖 UTF-8 文件,所以我只转换了字符串的编码:

$text = htmlentities(mb_convert_encoding($text, 'UTF-8', 'ASCII'), ENT_SUBSTITUTE, "UTF-8");

I had a similar issue and used the flag ENT_SUBSTITUTE to prevent the empty string. It still didn't encode, and I couldn't rely on the file being UTF-8, so I converted the encoding on just the string:

$text = htmlentities(mb_convert_encoding($text, 'UTF-8', 'ASCII'), ENT_SUBSTITUTE, "UTF-8");
深府石板幽径 2024-12-27 02:59:58

如果源文件包含该字符串,请确保将其保存为 UTf-8。否则,请确保提供字符串的任何内容都将其作为 UTF-8 提供。

Make sure you save your source file as UTf-8 if it contains the string. Else make sure that whatever is supplying the string supplies it as UTF-8.

阳光下慵懒的猫 2024-12-27 02:59:58
  1. 打开代码编辑器(例如记事本++或其他)。
  2. 单击新建>另存为.. 输入文件名(暂时空白),然后输入选择 PHP Hypertext ...
  3. 现在复制原始文件的所有内容并放入这个新文件中。
  4. 单击保存并尝试。
  1. Open your code editor (notepad++ for instance or other).
  2. Click in New > Save As.. put the name of file (blank for a while) and in type select PHP Hypertext ...
  3. Now copy all content of your original file and put in this new file.
  4. Click in save and try.
む无字情书 2024-12-27 02:59:58

只是为可能与我有类似问题的其他人发表评论。 $var 包含特殊字符。这……

<?= htmlentities($var) ?>

给了我一个空的输出,而……

<?php echo htmlentities($var); ?>

工作得很好。与 htmlspecialchars 相同。以前从未遇到过这种情况,因为我通常不会那样使用它,但在这里我只是测试一些东西。

Just commenting for others who may have a similar problem as myself. $var containing special characters. This …

<?= htmlentities($var) ?>

… gave me an empty output, while …

<?php echo htmlentities($var); ?>

… worked fine. Same with htmlspecialchars. Never encountered this before because I normally don't use it that way but here I was just testing something out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文