如何使用php转换xml中的特殊字符?
这个问题有很多帖子。我经历了所有这些,但我没有得到我预期的解决方案。我需要将XML中的特殊字符转换为html实体 我试过了如何使用php转换xml中的特殊字符?,php,xml,html,Php,Xml,Html,这个问题有很多帖子。我经历了所有这些,但我没有得到我预期的解决方案。我需要将XML中的特殊字符转换为html实体 我试过了 <?php $xml="<test>This is a xml file which has special characters < > & in it</test>"; htmlspecialchars($xml, ENT_XML1, 'UTF-8');//it replaces xml tags too ?>
<?php
$xml="<test>This is a xml file which has special characters < > & in it</test>";
htmlspecialchars($xml, ENT_XML1, 'UTF-8');//it replaces xml tags too
?>
预期的输出XML字符串:
<test>This is a xml file which has special characters < $gt; & in it</test>
这是一个xml文件,具有特殊字符$gt&;在里面
这个问题有很多帖子。我经历了所有这些,但我没有得到我预期的解决方案
是的,你认为你要问的话题已经很明确了,并且在这个网站上已经有了很好的介绍。然而,这并不能防止犯错误,因为它每天都发生在最好的使用环境中
在代码示例中编写:
这是一个具有特殊字符的xml文件
然后给出以下字符串:
<test>This is a xml file which has special characters < > & in it</test>
如本例所示,您选择的编码函数并非完全错误,它正确地编码了您要求的字符:
<test>This is a xml file which has special characters < > & in it</test>
你可能想换一种方式。查看上面的链接问题作为更好的搜索条件的开始,并查看其他人对此主题的提问和回答。$test=iconv('utf-8','ascii//TRANSLIT',$xml);请尝试这个实际上只是一个例子,我提到的tat字符串是xml。.我的问题是如何转换xml文件中的特殊字符。我将通过你建议的链接我尝试了
tidy\u-repair\u字符串($xml,['input-xml'=>1,'output-xml'=>1,'wrap'=>0])代码>并且它的工作方式与我预期的一样。多谢各位
<test>This is a xml file which has special characters < > & in it</test>
tidy_repair_string($xml, ['input-xml' => 1, 'output-xml' => 1, 'wrap' => 0]);