SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    Xbox why have you forsaken me? moospot's Avatar
    Join Date
    Feb 2001
    Location
    Clearwater, FL
    Posts
    3,615
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    String Parsing Replace Function

    I need to come up with a string parsing script that will remove invalid characters from Form submissions. I need to remove any ?, ' , *, etc. from the form fields before they go into the database. I saw a thread on this earlier, but can't seem to find it.

    <edit>
    Would I just do this:


    str1 = "This?is'my*story!"
    Replace(str1, "*", " "))
    Replace(str1, "'", " ")) 'single apostrophe
    Replace(str1, "?", " "))

    </edit>
    Last edited by moospot; Jul 25, 2001 at 09:46.

  2. #2
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,294
    Mentioned
    123 Post(s)
    Tagged
    1 Thread(s)

    Re: String Parsing Replace Function

    Close. You would actually do this:

    str1 = "This?is'my*story!"
    str1 = Replace(str1, "*", " "))
    str1 = Replace(str1, "'", " ")) 'single apostrophe
    str1 = Replace(str1, "?", " "))

    You might also want to do this in certain situations:
    str1 = replace(str1, ">", "&gt;")
    str1 = replace(str1, "<", "&lt;")

    <edit>I hate when I cut and paste and don't catch all the changes...
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style
    Learn how to be ready for The Forums' Move to Discourse

  3. #3
    SitePoint Guru
    Join Date
    Sep 1999
    Location
    Singapore
    Posts
    854
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh you could try some regular expressions. I like it a lot because the syntax is neater.

  4. #4
    Xbox why have you forsaken me? moospot's Avatar
    Join Date
    Feb 2001
    Location
    Clearwater, FL
    Posts
    3,615
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do you mean, duckie?

    I guess I need to know if this is the most efficiect way to do this as there are multiple form fields that will be validated.

    Is there a way to check them all at once and replace the invalids, or is it best to run them through one line at a time?

  5. #5
    SitePoint Guru
    Join Date
    Sep 1999
    Location
    Singapore
    Posts
    854
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Regular Expressions is a powerful string matching technique.

    What you would do would be something like this:

    Code:
    str1 = "This?is'my*story!"
    
    Dim objRegExpr
    Set objRegExpr = New regexp
    
    objRegExpr.Pattern = "['\*\?]"
    objRegExpr.Global = True
    objRegExpr.IgnoreCase = True
    
    str1 = objRegExpr.Replace(str1, " ")
    These two lines create your RegExp object:

    Code:
    Dim objRegExpr
    Set objRegExpr = New regexp
    In the next line you define your "pattern", which is the characters you want to match. In this case, the square brackets indicates match any one of the characters. The backslashes indicate that the next character is escaped. We need to do this as * and ? are special characters in Regular Expressions.

    The last line basically says, replace in str1 all matches with a space.

    Regular Expressions can get pretty confusing early on and it'll take a while before you get the hang of it.

    For more information you could check 4 Guys From Rolla and MSDN.
    Last edited by duckie; Jul 25, 2001 at 10:53.

  6. #6
    Xbox why have you forsaken me? moospot's Avatar
    Join Date
    Feb 2001
    Location
    Clearwater, FL
    Posts
    3,615
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think that hit the nail on the head! Thanks a million, Duckie!

  7. #7
    SitePoint Guru
    Join Date
    Sep 1999
    Location
    Singapore
    Posts
    854
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Keep in mind that it is more processor intensive than the Replace function.

    Also, the Global property, if set to True, says to not to stop after the first instance of the match encountered. As in, if set to False, the operation would end after the first match is done.

  8. #8
    Xbox why have you forsaken me? moospot's Avatar
    Join Date
    Feb 2001
    Location
    Clearwater, FL
    Posts
    3,615
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Would it be more processor intensize than 10 replace statements looking for an invalid character in each statement?

  9. #9
    SitePoint Guru
    Join Date
    Sep 1999
    Location
    Singapore
    Posts
    854
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That I really have no idea but I would tend to say the 10 Replace statements is more intensive. An interesting thing to note is that in an ASP page JScript does Regular Expressions much faster than VBScript (true as of 5.0 of the Windows Scripting Engine) so it is probably due to the immaturity of the Regular Expressions implementation in VBScript. Maybe speed has improved as of 5.5 (latest "Gold" release).

  10. #10
    Xbox why have you forsaken me? moospot's Avatar
    Join Date
    Feb 2001
    Location
    Clearwater, FL
    Posts
    3,615
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Regarding Processor Intensive: That's what it sounds like to me, too.

    I read that Regular Expressions were used in JScript long before VBScript.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •