Jump to content

parsing html parameters


Recommended Posts

Hello,

i need to get some info from an URL. The end of the URL looks similar to this:

/mycommand?name=Tester&phone=12345&mobile=544567

in my script i have variables $name, $phone and $mobile and i need the values from the url in the correct variables, even when the order of the parameters (the part after the ?) in the url is changed.

I made some ugly code to do that and it works fine, i would just like to find a better way.

Can anyone come up with a good idea here? Thank you!

 

Link to comment
Share on other sites

OK, here it comes, but beware, i know it is ugly .-)

Different from the discription in post #1 the example uses phone numbers only, but that is ok, i need it that way.

In the original code all the different bad request cases are used for differen error messages, i just removed them here to make it more readable.

$badrequest = 0
$fax = ""
$phone = ""
$mobile = ""
$sRequest = "/function?fax=12345&phone=67890&mobile=0123456"
$tempreq = StringReplace($sRequest, "/function?", "")
$afirstsplit = StringSplit($tempreq, "&") ;better some regexp?
If IsArray($afirstsplit) Then
    If $afirstsplit[0] = 3 Then
        For $i = 1 To 3
            If $badrequest = 1 Then ExitLoop
            $asecondsplit = StringSplit($afirstsplit[$i], "=")
            If IsArray($asecondsplit) Then
                If $asecondsplit[0] = 2 Then
                    If $asecondsplit[1] = "fax" Then
                        If $fax = "" Then
                            $fax = StringRegExpReplace($asecondsplit[2], "[D]", "") ;i want numbers only
                        Else
                            $badrequest = 1
                            ExitLoop
                        EndIf
                    ElseIf $asecondsplit[1] = "phone" Then
                        If $mobile = "" Then
                            $mobile = StringRegExpReplace($asecondsplit[2], "[D]", "")
                        Else
                            $badrequest = 1
                            ExitLoop
                        EndIf
                    ElseIf $asecondsplit[1] = "mobile" Then
                        If $phone = "" Then
                            $phone = StringRegExpReplace($asecondsplit[2], "[D]", "")
                        Else
                            $badrequest = 1
                            ExitLoop
                        EndIf
                    Else
                        $badrequest = 1
                        ExitLoop
                    EndIf
                Else
                    $badrequest = 1
                EndIf
            Else
                $badrequest = 1
            EndIf
        Next
    Else
        $badrequest = 1
    EndIf
Else
    $badrequest = 1
EndIf

If $badrequest = 0 Then
    ConsoleWrite("Phone:" & $phone & @CRLF)
    ConsoleWrite("Phone:" & $fax & @CRLF)
    ConsoleWrite("Phone:" & $mobile & @CRLF)
Else
    ConsoleWrite("Bad request" & @CRLF)
EndIf

 

Edited by Allow2010
Link to comment
Share on other sites

@Allow2010
You could do something like this:

#include <Array.au3>
#include <StringConstants.au3>

Global $strString = "/function?fax=12345&phone=67890&mobile=0123456", _
       $strPattern = "\b\w+=\d+\b", _
       $arrResult, _
       $arrElements[1][2]


$arrResult = StringRegExp($strString, $strPattern, $STR_REGEXPARRAYGLOBALMATCH)
For $i = 0 To UBound($arrResult) - 1 Step 1
    _ArrayAdd($arrElements, StringReplace($arrResult[$i], "=", "|"))
Next

_ArrayDelete($arrElements, 0)
_ArrayDisplay($arrElements)

Comments:

Spoiler

\b (Word boundary): capture "whole words" only, so, anything that is not a word is not "captured";
\w (Word metacharacter): "capture" everything that's a word ([a-zA-Z0-9_]);
+ (Quantifier): "capture" the previous metacharacterfrom 1 to N times;
\d (Digit metacharacter): "capture" only digits ([0-9]);
+ (Quantifier): "capture" the previous metacharacter from 1 to N times;
\b (Word boundary): capture "whole words" only, so, anything that is not a word is not "captured".

:)

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

A funny workaround  :)

#include <Array.au3>

$strString = "/function?fax=12345&phone=67890&mobile=0123456"

IniWriteSection(@scriptdir &"\tmp.ini", "tmp", StringReplace(StringReplace($strString, "/function?", ""), "&", @LF))
$array = IniReadSection(@scriptdir &"\tmp.ini", "tmp")
FileDelete(@scriptdir &"\tmp.ini")

_ArrayDisplay($array)

 

Link to comment
Share on other sites

6 hours ago, Allow2010 said:

$mobile = StringRegExpReplace($asecondsplit[2], "[D]", "")

Beware that [D] doesn't do what you seem to expect: it means the 'D' character. You probably meant \D instead (no need of the square brackets either).

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

#include <Array.au3>
Local $t = TimerInit(), $sRequest = "/function?fax=12345&phone=6(78)90&mobile=012-34/56"
Local $a = s2a($sRequest)
_ArrayDisplay($a, TimerDiff($t))

Func s2a($s)
    Local $e = 0, $i = 0, $n, $b = StringSplit($s, "?=&", 0)
    Local $a[UBound($b) + 1][2]
    ReDim $b[$b[0] + 2] ; to avoid array errors
    For $n = $b[0] To 2 Step -1
        Switch $b[$n]
            Case "fax", "phone", "mobile"
                $i += 1
                $a[$i][0] = $b[$n]
                $a[$i][1] = Int(StringRegExpReplace($b[$n + 1], "\D+", "")) ; you may need "+01..." ?
                If StringLen($a[$i][1]) < 4 Then $e += 1 ; number is too short
        EndSwitch
    Next
    ReDim $a[$i + 1][2]
    $a[0][0] = $i ; count
    If Not $i Then $e += 1
    $a[0][1] = $e ; error
    Return SetError(0, $e, $a)
EndFunc

 

Edited by argumentum
better fit your needs

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...