Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php XHTML文档的所有文本节点上的htmlentities_Php_Encoding_Domdocument_Html Entities - Fatal编程技术网

Php XHTML文档的所有文本节点上的htmlentities

Php XHTML文档的所有文本节点上的htmlentities,php,encoding,domdocument,html-entities,Php,Encoding,Domdocument,Html Entities,尝试使用数字实体对XHTML文档的所有文本节点进行编码。使用saveXML()可以得到十六进制值,但我需要Ӓ严格的数值。它也不会对破折号进行编码,还会做一些奇怪的事情,比如将©转换为©(相当于Â;©;)。我有一点没有使用DOMDocument了,所以这可能是一个混乱,我猜我这里有一些字符编码问题。到目前为止,我得到的是: $doc = new DOMDocument(); // load file $doc->load($input); //

尝试使用数字实体对XHTML文档的所有文本节点进行编码。使用saveXML()可以得到十六进制值,但我需要
Ӓ严格的数值。它也不会对破折号进行编码,还会做一些奇怪的事情,比如将©转换为
©(相当于
Â;©;
)。我有一点没有使用DOMDocument了,所以这可能是一个混乱,我猜我这里有一些字符编码问题。到目前为止,我得到的是:

$doc = new DOMDocument();
// load file
$doc->load($input);
// options
$doc->preserveWhiteSpace = true;
$doc->resolveExternals = true;
$doc->formatOutput = true;

// new xPath
$xp = new DOMXPath($doc);
// set ns for xhtml
$xp->registerNamespace("html", "http://www.w3.org/1999/xhtml");
// get all nodes
$q = "//body/*";
$nodes = $xp->query($q);

foreach ($nodes as $n) { 
    $children = $n->childNodes; 
    foreach ($children as $child) { 
        echo htmlentities($child->nodeValue,ENT_QUOTES|ENT_XHTML,"UTF-8",false);
    }
}
只是在这一点回显值以进行检查。有些东西,比如破折号,没有编码,需要编码,它仍然在使用
½而不是
½


文档可能已经包含实体,因此无法对其进行双重编码,但仍需要将其更改为数值。我在这里遗漏了什么?

我打过类似的战斗,基本上放弃了,并手动为strtr()创建了一个交换值列表


不太理想,可能是多余的,但它在我想要的时候给了我想要的东西。

我也打过类似的战斗,基本上放弃了,并手动为strtr()创建了一个交换值列表

不太理想,可能是多余的,但它给了我想要的,当我想要的时候

$child->nodeValue = strtr($child->nodeValue, array('½'=>'½', '©'=>'©'));