OK, I've come up with the following function that returns an array
containing the keywords. However, in order for this to work, you need to set
some ground rules:
1. Don't mix delimiters for a phrase. This will work correctly:
Jack, Jill Jim, "Timothy Brown", 'Mary'
but this will not:
Jack, Jill Jim, "Timothy Brown', 'Mary'
2. If literal delimiter characters are used, then they must not match the
delimiters used. For example, this will work:
"O'Malley"
but this will not:
'O'Malley'
Also, if literal delimiter characters are used, all delimiters in the entire
list must be the same. This will work:
Jim, "Tom Brown", "Pat O'Malley"
This won't:
Jim, 'Tom Brown', "Pat O'Malley"
Anyways, the function appears below my signature. You can use this code to
test it:
Dim iCount, arResult, sWords
sWords="Jack, Jill Jim, ""Timothy Brown"", 'Mary'"
Response.Write sWords & "<BR>"
arResult= ParseKeywords(sWords)
if IsArray(arResult) then
for iCount = 0 to UBound(arResult)
Response.Write arResult(iCount) & "<BR>"
next
end if
HTH,
Bob Barrows
Function ParseKeywords(pKeywords)
Dim sKeywords,iQuotes, arQuoted(), i, j, k, sTmp, bQfound, bSQFound
dim iCommas, arCommas, arSpaces, bArrayDefined, arKeywords()
bArrayDefined = false
sKeywords = pKeywords
'first see if sKeywords contains quoted sections - if so, make
'sure they are paired, ie, there is an even number of quotes
iQuotes = len(sKeywords) - len(Replace(sKeywords,"""",""))
bQfound = false
if iQuotes > 0 then
if iQuotes mod 2 = 0 then
bQfound = true
redim arQuoted(iQuotes/2 - 1)
i=instr(sKeywords,"""")
k = 0
Do Until i = 0
j = instr(i+1,sKeywords,"""")
sTmp = mid(sKeywords,i,j+1-i)
arQuoted(k) = sTmp
k=k+1
sKeywords = replace(sKeywords,sTmp,"")
i=instr(sKeywords,"""")
Loop
for i = 0 to ubound(arQuoted)
arQuoted(i) = replace(arQuoted(i),"""","")
next
end if
end if
'now find single-quoted sections
iQuotes = len(sKeywords) - len(Replace(sKeywords,"'",""))
bSQFound = false
if iQuotes > 0 then
if iQuotes mod 2 = 0 then
bSQFound = true
if bQfound = false then
redim arQuoted(iQuotes/2 - 1)
k = 0
else
k = ubound(arQuoted) + 1
Redim preserve arQuoted(UBound(arQuoted) + iQuotes/2)
end if
i=instr(sKeywords,"'")
Do Until i = 0
j = instr(i+1,sKeywords,"'")
sTmp = mid(sKeywords,i,j+1-i)
arQuoted(k) = sTmp
k=k+1
sKeywords = replace(sKeywords,sTmp,"")
i=instr(sKeywords,"'")
Loop
for i = 0 to ubound(arQuoted)
arQuoted(i) = replace(arQuoted(i),"'","")
next
end if
end if
sKeywords = RTrim(sKeywords)
do until right(sKeywords,1) <> ","
sKeywords = rtrim(left(sKeywords,len(sKeywords)-1))
loop
'add quoted sections to result array
if bQfound or bSQFound then
redim arKeywords(UBound(arQuoted))
for i = 0 to ubound(arQuoted)
arKeywords(i) = arQuoted(i)
next
bArrayDefined = true
end if
'now process commas and spaces
iCommas = len(sKeywords) - len(Replace(sKeywords,",",""))
arCommas=split(sKeywords,",")
for i = 0 to ubound(arCommas)
arCommas(i) = RTrim(LTrim(arCommas(i)))
if len(arCommas(i)) > 0 then
if instr(arCommas(i)," ") = 0 then
if bArrayDefined then
redim preserve arKeywords(UBound(arKeywords) + 1)
else
redim arKeywords(0)
end if
arKeywords(ubound(arKeywords)) = arCommas(i)
else
arSpaces = split(arCommas(i)," ")
for j = 0 to ubound(arSpaces)
arSpaces(j) = RTrim(LTrim(arSpaces(j)))
if len(arSpaces(j)) > 0 then
if bArrayDefined then
redim preserve arKeywords(UBound(arKeywords) + 1)
else
redim arKeywords(0)
end if
arKeywords(ubound(arKeywords)) = arSpaces(j)
end if
next
end if
end if
next
ParseKeywords=arKeywords
end function