DOMDocument对我的字符串做什么? [英] What is DOMDocument doing to my string?

查看:47
本文介绍了DOMDocument对我的字符串做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$dom = new DOMDocument('1.0', 'UTF-8');

$str = '<p>Hello®</p>';

var_dump(mb_detect_encoding($str)); 

$dom->loadHTML($str);

var_dump($dom->saveHTML()); 

查看

string(5) "UTF-8"

string(158) "<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p>Hello&Acirc;&reg;</p></body></html>
"

为什么我的Unicode ®转换为&& reg; ,该如何阻止呢?

Why did my Unicode ® get converted to &Acirc;&reg; and how do I stop this?

我今天会发疯吗?

推荐答案

您的文本编辑器在UTF-8中说® ,但是文件中的字节用Latin-1(或类似的编码)表示® ,这就是PHP用来读取它的方式。使用字符实体引用将消除这种歧义。

Your text editor says "®" in UTF-8, but the bytes in the file say "®" in Latin-1 (or a similar encoding), which is what PHP is using to read it. Using the character entity reference will remove this ambiguity.

>>> print u'®'.encode('utf-8').decode('latin-1')
®

这篇关于DOMDocument对我的字符串做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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