Page 1 of 1
Help with extracting specific data from a string
Posted: Fri Dec 19, 2003 12:54 am
by vincenz
Im new to Purebasic (1 week old) and need some help extracting values out of a form post. If I have a string that has:
--------------1234567
Content-Disposition: form-data; name="val1"
test
--------------1234567
Content-Disposition: form-data; name="val2"
--------------1234567
Content-Disposition: form-data; name="val3"
moredata
--------------1234567
Content-Disposition: form-data; name="val4"
this is a test
--------------1234567
Content-Disposition: form-data; name="val5"
hello
--------------1234567
How do I get the values to a string like this:
val1=test&val2=&val3=moredata&val4=this is a test&val5=hello
Note the difficulties are:
"" within the string
spaces within the actual value
missing data from some values
Can someone please help? I want to see how good this forum is...
Thanks

Posted: Fri Dec 19, 2003 1:24 am
by Froggerprogger
I had the time...
Code: Select all
Teststr.s = "--------------1234567 Content-Disposition: form-Data; name="+Chr(34)+"val1"+Chr(34)+" test --------------1234567 Content-Disposition: form-Data; name="+Chr(34)+"val2"+Chr(34)+"--------------1234567 Content-Disposition: form-Data; name="+Chr(34)+"val3"+Chr(34)+"moredata --------------1234567 Content-Disposition: form-Data; name="+Chr(34)+"val4"+Chr(34)+" this is a test --------------1234567 Content-Disposition: form-Data; name="+Chr(34)+"val5"+Chr(34)+" hello --------------1234567 "
Procedure.s PrepareString(str.s)
Protected temp.l
Protected StrPosP.l : StrPosP = 1
Protected ActChar.s
Protected FindStr1.s : FindStr1 = "name="+Chr(34)
Protected FindStr2.s : FindStr2 = "--------------"
Protected ResultStr.s : ResultStr = ""
StrPosP = FindString(str, FindStr1, StrPosP)
While StrPosP < Len(str) And StrPosP <> 0
If Len(ResultStr) > 0
ResultStr + "&"
EndIf
Debug ResultStr
StrPosP + Len(FindStr1)
ActChar = ""
; 1st: get the variable name
temp = FindString(str, Chr(34), StrPosP)
If temp = 0 ; if there's an error in syntax, (missing " ), then copy all the rest to the result and end
ResultStr + Mid(str,StrPosP,Len(str)-StrPosP)
Break
Else
ResultStr + Mid(str,StrPosP,temp-StrPosP)
EndIf
StrPosP = temp + 1
; 2nd: get the value
temp = FindString(str, FindStr2, StrPosP)
If temp = 0 ; if there's an error in syntax, (missing -------------- ), then copy all the rest to the result and end
ResultStr + Mid(str,StrPosP,Len(str)-StrPosP)
Break
Else
ResultStr + "="+Trim(Mid(str,StrPosP,temp-StrPosP))
EndIf
StrPosP = FindString(str, FindStr1, StrPosP)
Wend
ProcedureReturn ResultStr
EndProcedure
Debug PrepareString(Teststr)
btw: please use the
Code: Select all
and the [quote] - tags as often as senseful to ensure an lightly understandable messagetext.
(was THAT correct English ?)
:wink:
It Works!
Posted: Sat Dec 20, 2003 12:35 am
by vincenz
Froggerprogger you are a legend!
The code works fine. All I had to do was strip out the lines eg:
EOL$=Chr(13)+Chr(10)
ResultStr=ReplaceString(ResultStr, EOL$, "")
and then URL Encode the result!
Thanks heaps! I really appreciate it. If you had PAYPAL I would send you $20 for your effort!!!

Re: It Works!
Posted: Sat Dec 20, 2003 12:54 am
by dontmailme
vincenz wrote:Thanks heaps! I really appreciate it. If you had PAYPAL I would send you $20 for your effort!!!

Jeeees, anything else you need
Welcome to the forum

Re: It Works!
Posted: Sat Dec 20, 2003 2:21 am
by freedimension
vincenz wrote:Froggerprogger you are a legend!
The code works fine. All I had to do was strip out the lines eg:
EOL$=Chr(13)+Chr(10)
ResultStr=ReplaceString(ResultStr, EOL$, "")
and then URL Encode the result!
Thanks heaps! I really appreciate it. If you had PAYPAL I would send you $20 for your effort!!!

Why not send it by good old snailmail?

Posted: Sat Dec 20, 2003 3:12 am
by Froggerprogger
Perhaps I could get a gigantic 20$-worth-cup of coffee when I'm in New Zealand ?
It's just around the corner from here - a BIG corner - a 12000 miles-corner.
Damn here it is 3 o'clock in the morning, I have to go to bed now and it is winter. Hmmmm. If you are really in New Zealand, you'll have there a nice summer's afternoon atm......
I need holiday. Have a nice christmas. Don't drink and drive.
EDIT:
I'll go to bed now and I will dream from the summer and the ocean.

snail mail
Posted: Sat Dec 20, 2003 5:22 am
by vincenz
Snail mail cash doesnt work too well...
How about, sign up for Egold? Then I'll transfer $20. Otherwise, shout yourself a drink on me...

)
I feel welcome to these forums. Thanks
