Visual Basic 6 function "preg_quote"

Go back

Below you'll find the source for the Visual Basic 6 function preg_quote.

Attribute VB_Name = "modPregQuote"
' These functions are downloaded from:
' http://www.stefanthoolen.nl/archive/vb6-functions/
' 
' You may freely distribute this file but please leave all comments, including this one, in it.
' 
' @Author Stefan Thoolen <mail@stefanthoolen.nl>

Option Explicit

''
' Searches for a string in another string and gives all data from that point
' Same syntax as the PHP function 'strstr'
' See also: http://www.php.net/manual/en/function.strstr.php
' @param    String  haystack        The full string we want to search
' @param    String  needle          The part we need
' @return   String                  All characters including and following the needle
' @author   Stefan Thoolen <mail@stefanthoolen.nl>
Public Function strstr(haystack As String, needle As String) As String
    Dim i As Integer, ret As String
    For i = 1 To Len(haystack)
        If Mid(haystack, i, Len(needle)) = needle Then ret = Mid(haystack, i, Len(haystack) - i + 1): i = Len(haystack)
    Next i
    strstr = ret
End Function

''
' Quote regular expression characters
' Same syntax as the PHP function 'preg_quote'
' See also: http://www.php.net/manual/en/function.preg-quote.php
' @param    String  tstr        The normal string
' @param    String  delimiter   The used delimiter for the regex
' @return   String              A preg-safe string
' @author   Stefan Thoolen <mail@stefanthoolen.nl>
Public Function preg_quote(tstr As String, Optional delimiter As String) As String
    Dim escapestring As String, retval As String, i As Integer, s As String
    ' The special regular expression characters are: . \ + * ? [ ^ ] $ ( ) { } = ! < > | :
    escapestring = ".\+*?[^]$(){}=!<>|:" & delimiter
    For i = 1 To Len(tstr)
        s = Mid(tstr, i, 1)
        If strstr(escapestring, s) <> "" Then s = "\" & s
        retval = retval & s
    Next i
    preg_quote = retval
End Function