Help with extracting specific data from a string

Just starting out? Need help? Post your questions and find answers here.
vincenz
New User
New User
Posts: 9
Joined: Fri Dec 19, 2003 12:33 am

Help with extracting specific data from a string

Post 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

:P
Website Creations Ltd
Creators of www.codelock.co.nz
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post 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:
vincenz
New User
New User
Posts: 9
Joined: Fri Dec 19, 2003 12:33 am

It Works!

Post 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!!! :lol:
Website Creations Ltd
Creators of www.codelock.co.nz
dontmailme
Enthusiast
Enthusiast
Posts: 537
Joined: Wed Oct 29, 2003 10:35 am

Re: It Works!

Post by dontmailme »

vincenz wrote:Thanks heaps! I really appreciate it. If you had PAYPAL I would send you $20 for your effort!!! :lol:
Jeeees, anything else you need ;)

Welcome to the forum :)
Paid up PB User !
freedimension
Enthusiast
Enthusiast
Posts: 613
Joined: Tue May 06, 2003 2:50 pm
Location: Germany
Contact:

Re: It Works!

Post 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!!! :lol:
Why not send it by good old snailmail? :D
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post 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. :P
%1>>1+1*1/1-1!1|1&1<<$1=1
vincenz
New User
New User
Posts: 9
Joined: Fri Dec 19, 2003 12:33 am

snail mail

Post 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... :o)

I feel welcome to these forums. Thanks
:D
Website Creations Ltd
Creators of www.codelock.co.nz
Post Reply