Ampersand entities

Discussion in 'Ruby' started by Miquel Oliete, Nov 13, 2006.

  1. Hi all

    How can I convert from utf-8 to HMTL ampersand entities and from HTML
    ampersand entities to utf-8 (I'm searching it a lot but I can found it)?

    Thanks in advance


    Miquel (a.k.a. Ton)
    Linux User #286784
    GPG Key : 4D91EF7F
    Debian GNU/Linux (Linux Wolverine 2.6.14)

    Welcome to the jungle, we got fun and games
    Guns n' Roses

    LLama Gratis a cualquier PC del Mundo.
    Llamadas a fijos y móviles desde 1 céntimo por minuto.
    Miquel Oliete, Nov 13, 2006
    1. Advertisements

  2. --------------enigE43E102F822553B82F87372A
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable

    iconv from UTF8 to UTF16, add up the two bytes, pray it wasn't a
    surrogate pair, and convert to hex?

    David Vallner

    Content-Type: application/pgp-signature; name="signature.asc"
    Content-Description: OpenPGP digital signature
    Content-Disposition: attachment; filename="signature.asc"

    Version: GnuPG v1.4.5 (MingW32)

    -----END PGP SIGNATURE-----

    David Vallner, Nov 13, 2006
    1. Advertisements

  3. UTF-8 to HTML convertion is trivial.
    HTML to UTF-8 is almost trivial, you just need to decide the set of
    supported &-entities. This example handles only  and &,
    but it should be obvious how to extend it to other entities (if you
    want to do so).

    class String
    def utf8_to_html
    gsub(/([^\000-\177])|(&)/u) {
    if $2
    sprintf("&#x%x;", $1.unpack("U")[0])
    def html_to_utf8
    gsub(/&(?:#x([0-9a-fA-F]+)|(amp));/) {
    if $2
    [$1.hex].pack "U"
    Tomasz Wegrzanowski, Nov 14, 2006
  4. Miquel Oliete

    Miquel Guest

    Thanks to everybody. I will try your answers tonight at home.


    LLama Gratis a cualquier PC del Mundo.
    Llamadas a fijos y móviles desde 1 céntimo por minuto.
    Miquel, Nov 14, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.