# How to replace special charracters

**URL:** <https://www.ruby-forum.com/t/how-to-replace-special-charracters/93066>\
**Category:** Rails\
**Created:** [April 27, 2007, 2:57am UTC](https://www.ruby-forum.com/t/how-to-replace-special-charracters/93066 "2007-04-27T02:57:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rmehta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ea666f/32.png) [@rmehta](https://www.ruby-forum.com/u/rmehta)\
**Post date:** [April 27, 2007, 2:57am UTC](https://www.ruby-forum.com/t/how-to-replace-special-charracters/93066/1 "2007-04-27T02:57:00Z")

</div>

From the form input I have some data which is inserted into database ,  
and this data may have some special characters which can cause problems  
in other parts of the system. Now before inserting into db, how can I  
replace those characters with acceptable chars? Is there a standard way  
to replace double quotes with single quotes, slash with hyphen etc.?

Thanks.

---

<div class="post-metadata">

**Author:** ![rmehta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ea666f/32.png) [@rmehta](https://www.ruby-forum.com/u/rmehta)\
**Post date:** [April 27, 2007, 4:05am UTC](https://www.ruby-forum.com/t/how-to-replace-special-charracters/93066/2 "2007-04-27T04:05:58Z")

</div>

> From the form input I have some data which is inserted into database ,  
> and this data may have some special characters which can cause problems  
> in other parts of the system. Now before inserting into db, how can I  
> replace those characters with acceptable chars? Is there a standard way  
> to replace double quotes with single quotes, slash with hyphen etc.?

Look into Tidy…

[http://tidy.rubyforge.org/](http://tidy.rubyforge.org/)

-philip

---

<div class="post-metadata">

**Author:** ![rmehta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ea666f/32.png) [@rmehta](https://www.ruby-forum.com/u/rmehta)\
**Post date:** [April 27, 2007, 4:54am UTC](https://www.ruby-forum.com/t/how-to-replace-special-charracters/93066/3 "2007-04-27T04:54:00Z")

</div>

If you are using it for db purpose, regarding the single quote issue,  
you  
can look at:

ActiveRecord::Base#sanitize\_sql

Or a more flexible replacement, try ‘gsub’:

clean\_string = “test”.gsub(/[regex]/, ‘[replaced character]’)

---

<div class="post-metadata">

**Author:** ![rmehta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ea666f/32.png) [@rmehta](https://www.ruby-forum.com/u/rmehta)\
**Post date:** [August 9, 2007, 7:48am UTC](https://www.ruby-forum.com/t/how-to-replace-special-charracters/93066/4 "2007-08-09T07:48:54Z")

</div>

hello…

```
   if i have a string '12-34-56-78', how can i replace the '78' with

```

‘91’…pls…help…

thanks
