dsgnze6572 2014-12-17 02:08
浏览 31
已采纳

口音和ñxml问题

I'm trying to create a document XML and I have problem with some characters. I need to replace accents and letter ñ.

The output of the following code:

header('Content-type: text/html; charset=utf-8'); 
var_dump($this->xml_entities_s("Relucí"));

It shows:
string 'Reducí'

When I try to create the XML:

header('Content-type: text/xml; charset=utf-8');
$output = '<?xml version="1.0" encoding="UTF-8"?>';
$output .= $this->xml_entities_s("Relucí");
echo $output;


It shows:
string 'Reducí' 
And I want this to show:
string 'Reduc&#237;'

I need to show the above because there is a site that get data from my site and they asked for getting data on that way in xml with &#237; so that it can be parsed correctly.

    private function xml_entities_s($string) {
           return str_replace(array("<",">",'"',"'","&","á","Á","é","É","í","Í","ó","Ó","ú","Ú","ñ","Ñ"),
                       array("&lt;","&gt;","&quot;","&apos;","&amp;","&#225;","&#193;","&#233;","&#201;","&#237;","&#205;","&#243;","&#211;","&#250;","&#218;","&#241;","&#209;"),
                       $string);
    }

Could you help with this? Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongzhong7299 2014-12-17 08:56
    关注

    You don't really need to encode characters. UTF-8 supports them. Only characters with a special meaning (like <) need to be encoded. If you're using DOM to generate the XML it will take care of it.

    If you want to generate an ASCII XML you can define that in the constructor:

    $dom = new DOMDocument('1.0', 'ASCII');
    $dom
      ->appendChild($dom->createElement('div'))
      ->appendChild($dom->createTextNode('Relucí'));
    
    echo $dom->saveXml();
    

    Output:

    <?xml version="1.0" encoding="ASCII"?>
    <div>Reluc&#237;</div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么