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.

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

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备