du5114 2014-10-14 03:31
浏览 16
已采纳

添加  使用PHP进入xml

I want to add triple spacing in my xml but browser is changing triple spacing to single. I have found   to be used to include spacing. I have a tab delimited text file and i am converting it to xml using php. In order to have triple spacing inside my title node i am doing like this.

 $xml->startElement('Products');
    while ($line = fgetcsv($fp, 0, "\t")) {

        $xml->startElement('Product');
        //replacing titlesingle space to triple space
        $title = str_replace(" ", "   ", $line[1]);
        $xml->writeElement('title', $title);
        .....
        $xml->endElement();
    }
    $xml->endElement();
}

But is   changed to   and title is something like this

<title>A&amp;#160;&amp;#160;&amp;#160;Test</title>

Basically php is changing & to amp but i want exact   so that i can have triple spacing in the title

<title>A   Test</title>

Any help??

  • 写回答

1条回答 默认 最新

  • doulu7921 2014-10-14 07:37
    关注

    It does not change the spacing. But in HTML/XML several whitespaces are usually (not always) rendered as a single space or a linebreak.

    #160; is the non breaking space. One of the uses is to separate the parts of a phone number. You don't want one of the spaces in a phone number rendered as a linebreak.

    The UTF-8 bytes for this character are \xC2\xA0.

    $xml->writeElement('foo', "foo\xC2\xA0&\xC2\xA0bar");
    

    If the XML document is encoding is ASCII, they will get encoded as entities.

    $xml = new XMLWriter();
    $xml->openMemory();
    $xml->startDocument('1.0', 'ASCII');
    
    $xml->writeElement('foo', "foo\xC2\xA0&\xC2\xA0bar");
    
    $xml->endDocument();
    echo $xml->outputMemory(TRUE);
    

    Output:

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

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟