UtterAccess HomeUtterAccess Wiki

Welcome Guest ( Log In | Register )

Custom Search
Edit Discussion
> Strip Illegal Characters    


Synopsis

The following function removes any illegal characters from the passed string and optionally replaces them with a user entered character. Add (or remove) any other characters you want to check for to strIllegalChars.

Related Functions: GetAlphaOnly and GetDigitsOnly

CODE
' Strip Illegal Characters
' http://www.utteraccess.com/wiki/Strip_Illegal_Characters
' Code courtesy of UtterAccess Wiki
' Licensed under Creative Commons License
' http://creativecommons.org/licenses/by-sa/3.0/
'
' You are free to use this code in any application,
' provided this notice is left unchanged.
'
' rev  date                          brief descripton
' 1.0  2010-10-23                    Writing files to disk that contain illegal file characters can cause sometimes obscure error message(s)
'
Public Function fStripIllegal(strCheck As String, Optional strReplaceWith As String = "") As String

   On Error GoTo StripIllErr
   'illegal file name characters included in default string are    ? [ ] / \ = + < > :; * " , '

   Dim intI As Integer
   Dim intPassedString As Integer
   Dim intCheckString As Integer
   Dim strChar As String
   Dim strIllegalChars As String
   Dim intReplaceLen As Integer

   If IsNull(strCheck) Then Exit Function

   strIllegalChars = "?[]/\=+<>:;,*" & Chr(34) & Chr(39)  'add/remove characters you need removed to this string

   intPassedString = Len(strCheck)
   intCheckString = Len(strIllegalChars)

   intReplaceLen = Len(strReplaceWith)

   If intReplaceLen > 0 Then   'a character has been entered to use as the replacement character
   
       If intReplaceLen = 1 Then   'check the character itself isn't an illegal character
       
           If InStr(strIllegalChars, strReplaceWith) > 0 Then
               MsgBox "You can't replace an illegal character with another illegal character", _
                      vbOKOnly + vbExclamation, "Invalid Character"
               fStripIllegal = strCheck
               Exit Function
           End If

       Else   'only one replacement character allowed

           MsgBox "Only one character is allowed as a replacement character", _
                  vbOKOnly + vbExclamation, "Invalid Replacement String"
           fStripIllegal = strCheck
           Exit Function
           
       End If
   End If

   If intPassedString < intCheckString Then

       For intI = 1 To intCheckString
           strChar = Mid(strIllegalChars, intI, 1)
           If InStr(strCheck, strChar) > 0 Then
               strCheck = Replace(strCheck, strChar, strReplaceWith)
           End If
       Next intI

   Else

       For intI = 1 To intPassedString
           strChar = Mid(strIllegalChars, intI, 1)
           If InStr(strCheck, strChar) > 0 Then
               strCheck = Replace(strCheck, strChar, strReplaceWith)
           End If
       Next intI

   End If

   fStripIllegal = Trim(strCheck)

StripIllErrExit:
   Exit Function

StripIllErr:
   MsgBox "The following error occured: " & Err.Number & vbCrLf _
        & Err.Description, vbOKOnly + vbExclamation, "Unexpected Error"

   fStripIllegal = strCheck

   Resume StripIllErrExit

End Function

Creative Commons License
Strip Illegal Characters by UtterAccess Wiki is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Editing and revision of the content is freely encouraged; for details, see Expected Usage.

Edit Discussion
Custom Search
Thank you for your support!
This page has been accessed 8,728 times.  This page was last modified 11:37, 30 October 2013 by Mark Davis. Contributions by Jack Leach and Alan Greenwood  Disclaimers