[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ampersand entities

Miquel Oliete

11/13/2006 10:49:00 PM

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.
http://es.voice...

4 Answers

David Vallner

11/13/2006 10:56:00 PM

0

Miquel Oliete wrote:
> 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
>

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

David Vallner

Tomasz Wegrzanowski

11/14/2006 12:19:00 AM

0

On 11/13/06, Miquel Oliete <ktalanet@yahoo.es> wrote:
> 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

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 &#xHEX; and &amp;,
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
"&amp;"
else
sprintf("&#x%x;", $1.unpack("U")[0])
end
}
end
def html_to_utf8
gsub(/&(?:#x([0-9a-fA-F]+)|(amp));/) {
if $2
"&"
else
[$1.hex].pack "U"
end
}
end
end

--
Tomasz Wegrzanowski [ http://t-a-w.blo... ]

Pau Garcia i Quiles

11/14/2006 7:21:00 AM

0

Hello,

Try this:

str = 'äñÇáè&#8364;'
str.unpack("U*").collect { |s| (s > 127 ? "&##{s};" : s.chr) }.join("")

--
Pau Garcia i Quiles
http://www.e...
(Due to the amount of work, I usually need 10 days to answer)


Quoting Miquel Oliete <ktalanet@yahoo.es>:

> 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.
> http://es.voice...
>
>



Miquel Oliete

11/14/2006 8:19:00 AM

0

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

Bye

On Tue, 14 Nov 2006 09:18:44 +0900
"Tomasz Wegrzanowski" <tomasz.wegrzanowski@gmail.com> wrote:

> On 11/13/06, Miquel Oliete <ktalanet@yahoo.es> wrote:
> > 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
>
> 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 &#xHEX; and &amp;,
> 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
> "&amp;"
> else
> sprintf("&#x%x;", $1.unpack("U")[0])
> end
> }
> end
> def html_to_utf8
> gsub(/&(?:#x([0-9a-fA-F]+)|(amp));/) {
> if $2
> "&"
> else
> [$1.hex].pack "U"
> end
> }
> end
> end
>




______________________________________________
LLama Gratis a cualquier PC del Mundo.
Llamadas a fijos y m?viles desde 1 c?ntimo por minuto.
http://es.voice...