Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Function MakeSQLSafe(ByVal sql As String) As String
- 'first i'd avoid putting quote chars in as they might be valid? just double them up.
- Dim strIllegalChars As String = "/?-^%{}[];$=*`#|&@<>()+,"
- 'replace single quotes with double so they don't cause escape character
- If sql.Contains("'") Then
- sql = sql.Replace("'", "''")
- End If
- 'need to double up double quotes from what I remember to get them through
- If sql.Contains("""") Then
- sql = sql.Replace("""", """""")
- End If
- 'remove illegal chars
- For Each c As Char In strIllegalChars
- If sql.Contains(c.ToString) Then
- sql = sql.Replace(c.ToString, "")
- End If
- Next
- Return sql
- End Function
- public static string CleanUrl(this string urlpart) {
- // convert accented characters to regular ones
- string cleaned = urlpart.Trim().anglicized();
- // do some pretty conversions
- cleaned = Regex.Replace(cleaned, " ", "-");
- cleaned = Regex.Replace(cleaned, "#", "no.");
- cleaned = Regex.Replace(cleaned, "&", "and");
- cleaned = Regex.Replace(cleaned, "%", "percent");
- cleaned = Regex.Replace(cleaned, "@", "at");
- // strip all illegal characters like punctuation
- cleaned = Regex.Replace(cleaned, "[^A-Za-z0-9- ]", "");
- // convert spaces to dashes
- cleaned = Regex.Replace(cleaned, " +", "-");
- // If we're left with nothing after everything is stripped and cleaned
- if (cleaned.Length == 0)
- cleaned = "no-description";
- // return lowercased string
- return cleaned.ToLower();
- }
- // Convert accented characters to standardized ones
- private static string anglicized(this string urlpart) {
- string beforeConversion = "àÀâÂäÄáÁéÉèÈêÊëËìÌîÎïÏòÒôÔöÖùÙûÛüÜçÇ’ñ";
- string afterConversion = "aAaAaAaAeEeEeEeEiIiIiIoOoOoOuUuUuUcC'n";
- string cleaned = urlpart;
- for (int i = 0; i < beforeConversion.Length; i++) {
- cleaned = Regex.Replace(urlpart, afterConversion[i].ToString(), afterConversion[i].ToString());
- }
- return cleaned;
- // Spanish : ÁÉÍÑÓÚÜ¡¿áéíñóúü"
- }
Add Comment
Please, Sign In to add comment