【问题标题】:Why is html_entity_decode() working for the first string, but not the second?为什么 html_entity_decode() 对第一个字符串起作用,而对第二个字符串不起作用?
【发布时间】:2019-02-09 19:50:37
【问题描述】:
$string1 = 'Cotton Lanyard ½"';
$string2 = 'Organic Cotton Lanyard ⅝"';

echo html_entity_decode($string1); //Cotton Lanyard ½"
echo html_entity_decode($string2); //Organic Cotton Lanyard ⅝"

不确定如何解决这个问题?

【问题讨论】:

  • 我认为你的字符集是问题

标签: php encoding character-encoding character decoding


【解决方案1】:

它适用于 HTML5 文档类型 flag

echo html_entity_decode($string2, ENT_HTML5);   // Organic Cotton Lanyard ⅝"

由于默认类型是 ENT_HTML401 (HTML 4.01),我想这意味着 ⅝ 实体没有在那里定义。

【讨论】:

  • 在我的设置中,复制/粘贴 OP 代码给出了正确的结果
  • @Ggg 很有趣。我将其粘贴到 3v4l.org 中进行验证,原始代码显示与 OP 观察到的行为相同
  • 我有 ⅝ 在 php 中。但是当我转换为 json 并将其存储为 JS 变量时,它就变成了 ⅝代码。
  • @AlexDonnelly 您的意思是在您的问题中使用html_entity_decode 进行转换后,您在带有⅝ 的字符串上使用json_encode,它在JSON 中显示为⅝?我认为你实际上会得到\u215d,除非你使用JSON_UNESCAPED_UNICODE。或者我误解了你的意思。
  • @Don't Panic 让我说得更清楚些。我最初问这个问题是因为我看到我的 javascript 变量输出我得到的是“有机棉挂绳 &frac58”,而不是我预期的“有机棉挂绳 ⅝”。所以我虽然,好的 PHP 有问题。但我在 php 方面意识到 html_entity_decode 实际上正在工作并且正在生成“有机棉挂绳⅝”,就像我预期的那样。但是当我将它添加到 JS 变量时,它会返回 ⅝
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-04
  • 2016-03-29
  • 2012-11-30
  • 1970-01-01
  • 2011-12-06
  • 2021-04-19
  • 2017-06-28
相关资源
最近更新 更多