# Addslashes()

**URL:** <https://www.ruby-forum.com/t/addslashes/107718>\
**Category:** Ruby\
**Created:** [June 25, 2007, 6:18pm UTC](https://www.ruby-forum.com/t/addslashes/107718 "2007-06-25T18:18:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![no137b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bb73d2/32.png) [@no137b](https://www.ruby-forum.com/u/no137b)\
**Post date:** [June 25, 2007, 6:18pm UTC](https://www.ruby-forum.com/t/addslashes/107718/1 "2007-06-25T18:18:02Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Is there a built-in function in Ruby similar to PHP’s addslashes()  
function?  
MySQL keeps breaking…  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQFGf+p/2vy7v+d+psQRAqeDAKCLHdMotVW85fMMrBTKAezUHxQdRACfXsDb  
gB5LfblLO7n3Gy+1kgo8mI4=  
=YLAI  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![no137b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bb73d2/32.png) [@no137b](https://www.ruby-forum.com/u/no137b)\
**Post date:** [June 25, 2007, 9:15pm UTC](https://www.ruby-forum.com/t/addslashes/107718/2 "2007-06-25T21:15:31Z")

</div>

“Fred P.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> Is there a built-in function in Ruby similar to PHP’s addslashes() function?  
> MySQL keeps breaking…

Well, this probably indicates that you’re doing sql by dynamically  
building statements, which is generally a frowned-upon practice in  
favor of using prepared statements with ? placeholders. However, if  
you’re absolutely determined to do this:

def addslashes(str)  
str.gsub(/[‘"\\x0]/,’\\\0’)  
end

def stripslashes(str)  
str.gsub(/\(.)/,‘\1’)  
end

---

<div class="post-metadata">

**Author:** ![no137b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bb73d2/32.png) [@no137b](https://www.ruby-forum.com/u/no137b)\
**Post date:** [June 25, 2007, 9:19pm UTC](https://www.ruby-forum.com/t/addslashes/107718/3 "2007-06-25T21:19:32Z")

</div>

Fred P. wrote:

> -----BEGIN PGP SIGNED MESSAGE-----  
> Hash: SHA1
> 
> Is there a built-in function in Ruby similar to PHP’s addslashes()  
> function?  
> MySQL keeps breaking…

There should be a #escape\_str or #quote method on the connection object  
that will do what you’re after.
