Guest User

Untitled

a guest
Jun 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. Public Function MakeSQLSafe(ByVal sql As String) As String
  2. 'first i'd avoid putting quote chars in as they might be valid? just double them up.
  3. Dim strIllegalChars As String = "/?-^%{}[];$=*`#|&@<>()+,"
  4. 'replace single quotes with double so they don't cause escape character
  5. If sql.Contains("'") Then
  6. sql = sql.Replace("'", "''")
  7. End If
  8. 'need to double up double quotes from what I remember to get them through
  9. If sql.Contains("""") Then
  10. sql = sql.Replace("""", """""")
  11. End If
  12. 'remove illegal chars
  13. For Each c As Char In strIllegalChars
  14. If sql.Contains(c.ToString) Then
  15. sql = sql.Replace(c.ToString, "")
  16. End If
  17. Next
  18.  
  19. Return sql
  20. End Function
  21.  
  22. public static string CleanUrl(this string urlpart) {
  23.  
  24. // convert accented characters to regular ones
  25. string cleaned = urlpart.Trim().anglicized();
  26.  
  27. // do some pretty conversions
  28. cleaned = Regex.Replace(cleaned, "&nbsp;", "-");
  29. cleaned = Regex.Replace(cleaned, "#", "no.");
  30. cleaned = Regex.Replace(cleaned, "&", "and");
  31. cleaned = Regex.Replace(cleaned, "%", "percent");
  32. cleaned = Regex.Replace(cleaned, "@", "at");
  33.  
  34. // strip all illegal characters like punctuation
  35. cleaned = Regex.Replace(cleaned, "[^A-Za-z0-9- ]", "");
  36.  
  37. // convert spaces to dashes
  38. cleaned = Regex.Replace(cleaned, " +", "-");
  39.  
  40. // If we're left with nothing after everything is stripped and cleaned
  41. if (cleaned.Length == 0)
  42. cleaned = "no-description";
  43.  
  44. // return lowercased string
  45. return cleaned.ToLower();
  46. }
  47.  
  48. // Convert accented characters to standardized ones
  49. private static string anglicized(this string urlpart) {
  50. string beforeConversion = "àÀâÂäÄáÁéÉèÈêÊëËìÌîÎïÏòÒôÔöÖùÙûÛüÜçÇ’ñ";
  51. string afterConversion = "aAaAaAaAeEeEeEeEiIiIiIoOoOoOuUuUuUcC'n";
  52.  
  53. string cleaned = urlpart;
  54.  
  55. for (int i = 0; i < beforeConversion.Length; i++) {
  56. cleaned = Regex.Replace(urlpart, afterConversion[i].ToString(), afterConversion[i].ToString());
  57. }
  58. return cleaned;
  59.  
  60. // Spanish : ÁÉÍÑÓÚÜ¡¿áéíñóúü"
  61.  
  62. }
Add Comment
Please, Sign In to add comment