dounianxie2058 2013-08-22 11:52
浏览 57
已采纳

too long

I have a dom document to which I would like to append some special characters.

What I am doing is first to detect the special character in a string with a regexp. For that, I am transforming the utf-8 characters on html with

$string = "Test string 1,§,†,‡";
$string_html = htmlentities($string_html, ENT_QUOTES, "UTF-8");

This works fine and I am getting there:

"Test string 1,§,†,‡"

Now, I would like to get the special characters only. I am using for that a regexp which is returning an array with:

[0] => '&sect';
[1] => '&dagger';
[2] => '&Dagger';

Now, I would like to append those special characters to my dom document, but in unicode. I was trying:

$string_utf8 = html_entity_decode($string_html);

$dom_output->createElement( 'string', utf8_encode($string_utf8));

The results are the next:

The § character is displayed as §, and the † and ‡ are staying as that.

Any idea why?

  • 写回答

1条回答 默认 最新

  • douling1936 2013-08-22 12:14
    关注

    The issue with § is that it is at position 0xA7 in the unicode table. However, that particular character is encoded as the two-byte sequence 0xC2A7. Your output is being rendered as ISO-8859-1 or similar encoding, resulting in this  (0xC2) appearing before your intended character (since coincidentally it has the same last byte - it needn't, I frequently see é encoded as é for example)

    However the dagger and double-dagger are much further in the unicode table: 0x2020 and 0x2021 respectively. As such, there is no confusion when it comes to encoding them.

    I think the problem may be caused by the PHP file itself being encoded in a single-byte character set, and it is this that is causing the issues with 0xA7. Make sure your PHP file is encoded in the correct format, otherwise it will make incorrect assumptions about your strings.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。