# Ampersand entities

**URL:** <https://www.ruby-forum.com/t/ampersand-entities/75878>\
**Category:** Ruby\
**Created:** [November 13, 2006, 11:51pm UTC](https://www.ruby-forum.com/t/ampersand-entities/75878 "2006-11-13T23:51:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Miquel\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@Miquel\_O](https://www.ruby-forum.com/u/Miquel_O)\
**Post date:** [November 13, 2006, 11:51pm UTC](https://www.ruby-forum.com/t/ampersand-entities/75878/1 "2006-11-13T23:51:04Z")

</div>

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.yahoo.com](http://es.voice.yahoo.com)

---

<div class="post-metadata">

**Author:** ![Miquel\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@Miquel\_O](https://www.ruby-forum.com/u/Miquel_O)\
**Post date:** [November 13, 2006, 11:57pm UTC](https://www.ruby-forum.com/t/ampersand-entities/75878/2 "2006-11-13T23:57:05Z")

</div>

Miquel O. 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 V.

---

<div class="post-metadata">

**Author:** ![Miquel\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@Miquel\_O](https://www.ruby-forum.com/u/Miquel_O)\
**Post date:** [November 14, 2006, 1:19am UTC](https://www.ruby-forum.com/t/ampersand-entities/75878/3 "2006-11-14T01:19:16Z")

</div>

On 11/13/06, Miquel O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 &,  
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  
“&”  
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

---

<div class="post-metadata">

**Author:** ![Miquel\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@Miquel\_O](https://www.ruby-forum.com/u/Miquel_O)\
**Post date:** [November 14, 2006, 8:21am UTC](https://www.ruby-forum.com/t/ampersand-entities/75878/4 "2006-11-14T08:21:47Z")

</div>

Hello,

Try this:

str = ‘äñÇáè€’  
str.unpack(“U\*”).collect { |s| (s \> 127 ? “&##{s};” : s.chr) }.join(“”)

–  
Pau Garcia i Quiles  
[http://www.elpauer.org](http://www.elpauer.org)  
(Due to the amount of work, I usually need 10 days to answer)

Quoting Miquel O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![Miquel\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@Miquel\_O](https://www.ruby-forum.com/u/Miquel_O)\
**Post date:** [November 14, 2006, 9:19am UTC](https://www.ruby-forum.com/t/ampersand-entities/75878/5 "2006-11-14T09:19:53Z")

</div>

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

Bye

On Tue, 14 Nov 2006 09:18:44 +0900  
“Tomasz W.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> HTML to UTF-8 is almost trivial, you just need to decide the set of  
> sprintf(“&#x%x;”, $1.unpack(“U”)[0])  
> }  
> end  
> end

* * *

LLama Gratis a cualquier PC del Mundo.  
Llamadas a fijos y móviles desde 1 céntimo por minuto.  
[http://es.voice.yahoo.com](http://es.voice.yahoo.com)
