Posted By

Scooter on 02/21/09


Tagged


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

wizard04
benrudolph


Add and strip slashes


 / Published in: ASP
 

URL: http://reusablecode.blogspot.com/2009/02/add-and-strip-slashes.html

ASP implementation on the PHP functions by the same name, but with support for more problematic characters.

  1. <%
  2. ' Copyright (c) 2009, reusablecode.blogspot.com; some rights reserved.
  3. '
  4. ' This work is licensed under the Creative Commons Attribution License. To view
  5. ' a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
  6. ' send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
  7. ' 94305, USA.
  8.  
  9. ' Despite the identical naming, these functions are more comprehensive than their PHP equivalents.
  10. ' They go above and beyond even mysql_real_escape_string(), by including support for backspace and horizontal tab.
  11.  
  12. ' List of characters handled:
  13. ' \000 null
  14. ' \010 backspace
  15. ' \011 horizontal tab
  16. ' \012 new line
  17. ' \015 carriage return
  18. ' \032 substitute
  19. ' \042 double quote
  20. ' \047 single quote
  21. ' \134 backslash
  22. ' \140 grave accent
  23.  
  24. ' Returns a string with backslashes before characters that need to be quoted in database queries
  25. function addslashes(unsafeString)
  26. dim regEx
  27.  
  28. set regEx = new RegExp
  29.  
  30. with regEx
  31. .Global = true
  32. .IgnoreCase = true
  33. .Pattern = "([\000\010\011\012\015\032\042\047\134\140])"
  34. end with
  35.  
  36. addslashes = regEx.replace(unsafeString, "\$1")
  37.  
  38. set regEx = nothing
  39. end function
  40.  
  41. ' Un-quote string quoted with addslashes()
  42. function stripslashes(safeString)
  43. dim regEx
  44.  
  45. set regEx = new RegExp
  46.  
  47. with regEx
  48. .Global = true
  49. .IgnoreCase = true
  50. .Pattern = "\\([\000\010\011\012\015\032\042\047\134\140])"
  51. end with
  52.  
  53. stripslashes = regEx.replace(safeString, "$1")
  54.  
  55. set regEx = nothing
  56. end function
  57. %>

Report this snippet  

You need to login to post a comment.