Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Aug 2005
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs down PHP returns invalid XML because of '&'

    I'm writing a PHP script to query a database table and return the result as XML. If the result contain the letter '&', the XML is corrupted. How can I fix this problem?
    <xml>
    <record id='2' text='jjjj&kkkk'>any</record>
    </xml>

  • #2
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    Encode it as &amp;

  • #3
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    Encode it as &amp; in the stored XML file.

    e.g.
    Code:
    <xml>
    <record id='2' text='jjjj&amp;kkkk'>any</record>
    </xml>
    The htmlspecialchars and htmlentities won't always do the job properly, so I use my owned, tweaked option…

    PHP Code:
    function xmlspecialchars($str) {

        return 
    str_replace('&#039;','&apos;',htmlspecialchars($str,ENT_QUOTES,'UTF-8'));




  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •