PHP XML Entity Encoding issue

前端 未结 1 907
走了就别回头了
走了就别回头了 2021-01-24 04:15

Having spent a few hours researching I have been unable to come up with an answer for this. I am trying to send an XML string to a third party so I need to encode some character

相关标签:
1条回答
  • 2021-01-24 04:24

    You need to use a createEntityReference function to get it to pass through unmolested.

    Example:

    $XMLDoc = new DOMDocument('1.0', 'utf-8');
    $comments = $XMLDoc->createElement('Comments');
    
    $text1 = $XMLDoc->createTextNode('An apostrophe here < ');
    $text2 = $XMLDoc->createEntityReference('apos');
    $text3 = $XMLDoc->createTextNode(' > Pound sign: ');
    $text4 = $XMLDoc->createEntityReference('pound');
    
    $comments->appendChild($text1);
    $comments->appendChild($text2);
    $comments->appendChild($text3);
    $comments->appendChild($text4);
    $XMLDoc->appendChild($comments);
    echo $XMLDoc->saveXML();
    

    Note that some items are encoded by default by createTextNode. < and > are encoded, pound signs are not. Hope this helps!

    0 讨论(0)
提交回复
热议问题