Using the British pound sign in an XML feed to be read by an iPhone

17,114

Solution 1

My final solution which seems to work in all cases is to replace all special chars as they are input.

  public function xmlEntities($text)
  {
    $search = array('&','<','>','"','\'', chr(163), chr(128), chr(165));
    $replace = array('&amp;', '&lt;', '&gt;', '&quot;', '&apos;', '&#163;', '&#8364;', '&#165;');

    $text = str_replace($search, $replace, $text);

    return $text;
  }

Solution 2

if the feed contains a British Pound sign, I get a nasty XML error: XML Parsing Error: undefined entity

Your feed probably is using entity &pound; as the pound character. &pound; is a HTML entity and those can't be used without declaring them with DTD associated with (or embedded in) your XML document. If the entity is not defined, the XML parser will report that it has found an unknown entity.

Since you said your feed is encoded as UTF-8, you can just use the pound character as such - no need for an entity. Like LukeH suggested, other solution is to use the character reference &#163; which will be read as pound character by the XML parser.

Solution 3

You could just use the &#163; entity.

Solution 4

As you've shown a PHP code snippet...

I had the same problem, but your sample xmlEntities($text) { ... } function above didn't work.

Changing to just htmlspecialchars() did work however - as (in my case) I only care about characters which would screw up the parsing of the XML document (<,> etc), everything else should be valid unicode ....

Share:
17,114

Related videos on Youtube

Jon Winstanley
Author by

Jon Winstanley

Web Developer Web developer, London I am the developer behind the popular domain expiry reminder service Skinny Domain PHP, Symfony, Laravel Javascript, React, React Native, jQuery HTML5 &amp; CSS3 MySQL, MariaDB, Postgres

Updated on May 05, 2022

Comments

  • Jon Winstanley
    Jon Winstanley almost 2 years

    I have created a web-based UTF-8 XML feed for use in an iPhone application.

    When viewing in a web browser, if the feed contains a British Pound sign, I get a nasty XML error: XML Parsing Error: undefined entity

    However the actual file seems to be readable.

    1. Will an iPhone NSParser be able to read the file or will it fail due to this character?

    2. Is it possible to encode the pound sign for XML?

    • McDowell
      McDowell over 13 years
      check that the pound sign is saved as the byte sequence c2 a3 using a hex editor. If not, the text is being saved as some other encoding.