# Convert HTML code of single quote (i.e. ') to text

**URL:** <https://www.ruby-forum.com/t/convert-html-code-of-single-quote-i-e-to-text/174792>\
**Category:** Rails\
**Created:** [September 2, 2009, 10:08am UTC](https://www.ruby-forum.com/t/convert-html-code-of-single-quote-i-e-to-text/174792 "2009-09-02T10:08:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nam\_gi\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47774/32.png) [@Nam\_gi\_V](https://www.ruby-forum.com/u/Nam_gi_V)\
**Post date:** [September 2, 2009, 10:08am UTC](https://www.ruby-forum.com/t/convert-html-code-of-single-quote-i-e-to-text/174792/1 "2009-09-02T10:08:38Z")

</div>

When I save a single quote character in my database, it was converted  
into & so when I read it out from database to view on my webpage,  
this single quote character (and maybe many other special-treated  
character) is showed as & which is not expected. I expect the " ’ "  
character to be displayed.

How can I convert & to ’ (single quote character) using ruby code?

Please help.  
Nam.

---

<div class="post-metadata">

**Author:** ![Nam\_gi\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47774/32.png) [@Nam\_gi\_V](https://www.ruby-forum.com/u/Nam_gi_V)\
**Post date:** [September 2, 2009, 12:56pm UTC](https://www.ruby-forum.com/t/convert-html-code-of-single-quote-i-e-to-text/174792/2 "2009-09-02T12:56:09Z")

</div>

I don’t see why this is an issue for you. & is the right  
representation, the browser takes care of rendering that as a quote.  
You need to see what you are doing with the incoming data before  
displaying it. Also, have a look at html\_escape() / h() helper  
function and sanitize data before database insertion. You shouldn’t  
be storing a & into the database text field.

---

<div class="post-metadata">

**Author:** ![Nam\_gi\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47774/32.png) [@Nam\_gi\_V](https://www.ruby-forum.com/u/Nam_gi_V)\
**Post date:** [September 2, 2009, 1:55pm UTC](https://www.ruby-forum.com/t/convert-html-code-of-single-quote-i-e-to-text/174792/3 "2009-09-02T13:55:17Z")

</div>

Hi Mukund,  
Let me make it clearer to you.  
My situation is: I have a textbox to input the data named Username for  
example. The user input a string that contains the single quote, e.g.  
“Mukund’s name”, then I it was saved into MySQL database.

The text was converted automatically into “Mukund&s name” in the  
record inside the database. So, when I view that record on my webpage to  
edit it, the text inside my editbox is exactly “Mukund’s name”. I  
understand what you mean about the browser automatic convertion, but it  
won’t translate it inside the textbox.

I’m not clear aobut your advice to use html\_escape(). This helper will  
turns single quote " '" into '. What I need is the reverse  
direction: converting ’ back to single quote " '".

---

<div class="post-metadata">

**Author:** ![Nam\_gi\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47774/32.png) [@Nam\_gi\_V](https://www.ruby-forum.com/u/Nam_gi_V)\
**Post date:** [September 2, 2009, 3:43pm UTC](https://www.ruby-forum.com/t/convert-html-code-of-single-quote-i-e-to-text/174792/4 "2009-09-02T15:43:17Z")

</div>

I google “reverse + html\_escape” and found a thread in this forum about  
what I need here [Reverse of h() - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/908).  
I wouldn’t find that without your mention about the h() function. Thank  
you Mukund.
