DOM: accented characters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jonathan Mcdougall

    #1

    DOM: accented characters

    Using this xml file:

    // test.xml
    <?xml version="1.0"?>
    <test>é</test>

    (that's a &eacute;)

    and this script:

    $doc = DOMDocument::lo ad("test.xml") ;

    outputs

    Warning: DOMDocument::lo ad() [function.load]: Input is not proper
    UTF-8, indicate encoding ! in file:///test.xml, line: 2 in E:\test.php
    on line 3

    Warning: DOMDocument::lo ad() [function.load]: Bytes: 0xE9 0x3C 0x2F
    0x74 in file:///test.xml, line: 2 in E:\test.php on line 3

    I tried using htmlentities(), so the xml file looks like

    // test.xml
    <?xml version="1.0"?>
    <test>&eacute ;</test>

    The script does not change, the output is

    Warning: DOMDocument::lo ad() [function.load]: Entity 'eacute' not
    defined in file:///test.xml, line: 2 in E:\test.php on line 3

    I tried fiddling with the encoding, but it was more of a guess than
    anything. Can anyone solve this problem?

    Thank you,


    Jonathan

  • Tim Van Wassenhove

    #2
    Re: DOM: accented characters

    On 2005-08-26, Jonathan Mcdougall <jonathanmcdoug all@gmail.com> wrote:[color=blue]
    > Using this xml file:
    >
    > // test.xml
    ><?xml version="1.0"?>
    ><test>é</test>
    >
    > (that's a &eacute;)
    >
    > and this script:
    >
    > $doc = DOMDocument::lo ad("test.xml") ;[/color]
    [color=blue]
    > Warning: DOMDocument::lo ad() [function.load]: Bytes: 0xE9 0x3C 0x2F
    > 0x74 in file:///test.xml, line: 2 in E:\test.php on line 3[/color]

    0xE9 is ISO-8859-1 encoding for &acute; 0x3C for &lt;
    [color=blue]
    ><test>&eacute; </test>[/color]

    Might want to try <test>&#233;</test>

    --
    Met vriendelijke groeten,
    Tim Van Wassenhove <http://timvw.madoka.be >

    Comment

    • Jonathan Mcdougall

      #3
      Re: DOM: accented characters

      Tim Van Wassenhove wrote:[color=blue]
      > On 2005-08-26, Jonathan Mcdougall <jonathanmcdoug all@gmail.com> wrote:[color=green]
      > > Using this xml file:
      > >
      > > // test.xml
      > ><?xml version="1.0"?>
      > ><test>é</test>
      > >
      > > (that's a &eacute;)
      > >
      > > and this script:
      > >
      > > $doc = DOMDocument::lo ad("test.xml") ;[/color]
      >[color=green]
      > > Warning: DOMDocument::lo ad() [function.load]: Bytes: 0xE9 0x3C 0x2F
      > > 0x74 in file:///test.xml, line: 2 in E:\test.php on line 3[/color]
      >
      > 0xE9 is ISO-8859-1 encoding for &acute; 0x3C for &lt;
      >[color=green]
      > ><test>&eacute; </test>[/color]
      >
      > Might want to try <test>&#233;</test>[/color]

      Is there a php function which translates special characters to
      character codes?
      htmlentities() only translates to named codes (&eacute;).

      Jonathan

      Comment

      • Malcolm Dew-Jones

        #4
        Re: DOM: accented characters

        Jonathan Mcdougall (jonathanmcdoug all@gmail.com) wrote:
        : Using this xml file:

        : // test.xml
        : <?xml version=3D"1.0" ?>
        : <test>=E9</test>

        I assume that the news posting software has encoded a byte as the string
        "=E9"

        : (that's a &eacute;)

        In the correct 8 bit character set that byte could be interpretted that
        way.

        : and this script:

        : $doc =3D DOMDocument::lo ad("test.xml") ;

        : outputs

        : Warning: DOMDocument::lo ad() [function.load]: Input is not proper
        : UTF-8, indicate encoding ! in file:///test.xml, line: 2 in E:\test.php
        : on line 3

        The xml parser assumes the data is encoded in utf-8. In utf-8, the byte
        with the value 0xE9 would have to be the start of a multi-byte sequence.
        By itself that byte is not a character (in utf-8).


        : Warning: DOMDocument::lo ad() [function.load]: Bytes: 0xE9 0x3C 0x2F
        : 0x74 in file:///test.xml, line: 2 in E:\test.php on line 3

        The multi-byte sequence would be the four bytes 0xE9 0x3C 0x2F 0x74, but
        that sequence is not in fact a valid utf-8 sequence.

        : I tried using htmlentities(), so the xml file looks like

        : // test.xml
        : <?xml version=3D"1.0" ?>
        : <test>&eacute ;</test>

        : The script does not change, the output is

        : Warning: DOMDocument::lo ad() [function.load]: Entity 'eacute' not
        : defined in file:///test.xml, line: 2 in E:\test.php on line 3

        No, xml does not know anything about entities, except the minimal set,
        which I recall as being &amp; &lt; &gt; &quot; &apos;

        Anything else must be declared (which has complications to use), or you
        can use the numeric "entity" (is that the right name?). I.e. &#n; where
        "n" is a decimal number, or &#xh; where h is a hexidecimal number.


        : I tried fiddling with the encoding, but it was more of a guess than
        : anything. Can anyone solve this problem?

        I assume you could define the correct 8bit character encoding in the
        <?xml...> thingy, though that may require the parser to know that
        character set.

        Or use a numeric "entity" to encode the non-ascii characters.

        Or encode using utf-8 (not sure which php function does that).



        --

        This programmer available for rent.

        Comment

        • Janwillem Borleffs

          #5
          Re: DOM: accented characters

          Jonathan Mcdougall wrote:[color=blue]
          > Is there a php function which translates special characters to
          > character codes?
          > htmlentities() only translates to named codes (&eacute;).
          >[/color]

          You can use the translation table used by htmlentities() and
          htmlspecialchar s() as follows:

          $table = get_html_transl ation_table(HTM L_ENTITIES);
          foreach (array_keys($ta ble) as $k) {
          $table[$k] = '&#' . ord($k) . ';';
          }

          And then apply it as the second argument to strtr():

          $text = strtr($text, $table);


          JW



          Comment

          • Jonathan Mcdougall

            #6
            Re: DOM: accented characters

            Janwillem Borleffs wrote:[color=blue]
            > Jonathan Mcdougall wrote:[color=green]
            > > Is there a php function which translates special characters to
            > > character codes?
            > > htmlentities() only translates to named codes (&eacute;).
            > >[/color]
            >
            > You can use the translation table used by htmlentities() and
            > htmlspecialchar s() as follows:
            >
            > $table = get_html_transl ation_table(HTM L_ENTITIES);
            > foreach (array_keys($ta ble) as $k) {
            > $table[$k] = '&#' . ord($k) . ';';
            > }
            >
            > And then apply it as the second argument to strtr():
            >
            > $text = strtr($text, $table);[/color]

            Great, thank you, very good thing to know.

            Finally, I decided to back to xml_parser, which is less convinient, but
            accepts htmlentities and allows "selective" loading of an xml document,
            which fits my requirements.

            Thank to all,


            Jonathan

            Comment

            Working...