The VBScript PHP extract () function _vbs

Source: Internet
Author: User
Tags function prototype key string

PHP has been written to know that it has a extract () is very convenient, you can easily convert the dictionary to variables, of course, to the ASP will be limited to a lot of, especially VBScript script, this article describes the idea of a transformation, you can achieve similar functions.

Below I will directly provide the ASP version of the Extract Code bar:

Copy Code code as follows:

'
' Asp/vbscript Dictionary Extract
' Author:wangye
' For more information please visit
'
' This code is distributed under the BSD license
'
' Collection collection or dictionary, which can be accessed through the
' Request.Form or Request.QueryString
' specified specifies the attribute that must exist, and if the property does not exist, an automatically created
' Prefix the prefix modifier for each property
' Callback a function call for the values of each element (Key-value) of a collection or dictionary
' Function prototype:
' Function filter (key, value)
' Filter = value
' End If
' The final value will be the value returned by the function
'
Function Extract (collection, ByVal specified, prefix, callback)
Dim VarName, Varvalue, Dynobj, Searchkey
specified = "," & Replace (Specified, "", "") & ","

Set dynobj = New dynamicobject
For each key in collection
Searchkey = "," & Key & ","
If InStr (1, specified, Searchkey, 1) >0 Then
specified = Replace (specified, Searchkey, "")
If left (specified, 1) <> "," Then
specified = "," & Specified
End If
If right (specified, 1) <> "," Then
specified = specified & ","
End If
End If

VarName = prefix & key

Varvalue = collection (key)
If callback<> "" Then
Varvalue = GetRef (callback) (Key, Varvalue)
End If

Dynobj.add VarName, Varvalue, property_access_readonly
Next

Specified_array = Split (Specified, ",")
Dim I
For i = LBound (Specified_array) to UBound (Specified_array)
If Specified_array (i) <> "" Then
Dynobj.add prefix & Specified_array (i), "", _
Property_access_readonly
End If
Next
Set extract = Dynobj.getobject ()
End Function


To introduce the following methods of use:

Copy Code code as follows:

Dim Query
Set query = Extract (Request.QueryString, "Name,id", "", "")

Response.Write Query.name
Response.Write Query.id

Set query = Nothing

Access the ASP page that contains the above code, the QueryString (that is, after the URL question mark) contains Name=wangye you will see the page output "Wangye", including id=12, will output "12″, of course you can also specify two."

You may find that when you Response.Write output name and ID key, the program error, because the specified attribute does not exist, when you are in the query string contains this key, the program is normal, because this key automatically established properties, so you can directly Response.Write , how to avoid it?

1. Through the specified parameter of the extract () function, which is a comma-separated key string, you can see that this feature is used in the sample code, if the query string does not contain the corresponding key, but you use this key again, As long as there is in the specified list, the property is automatically created with a null value, so there is no error.

2. By returning the Hasattr_ method of the object, the method can be used to determine whether the object returned by the Extract () function has a corresponding attribute, such as the code:

Copy Code code as follows:

Dim Query
Set query = Extract (Request.QueryString, "Name,id", "", "")

If query.hasattr_ ("job") Then
Response.Write "Job:" & Query.job
End If

Set query = Nothing

The job is not in our specified list, but the direct Access program without the query string does not give an error because we are using HASATTR_ to determine whether this property exists before use.

3. Secure access by returning the Getattr_ method of the object, which determines whether the specified property exists before use, and replaces it with the default value if it does not exist, with a detailed reference to the DynamicObject description, such as code:

Copy Code code as follows:

Dim Query
Set query = Extract (Request.QueryString, "Name,id", "", "")

Response.Write "Job:" & Query.getattr_ ("Job", "No Job")

Set query = Nothing

Finally, the use of the filter, the filter parameter of the extract () function, specifies another function name string, and then extract () calls the function for each value, for example, the previous code:
Copy Code code as follows:

Dim name, job, ID
Name = Trim (Request.QueryString ("name"))
Job = Trim (Request.QueryString ("job"))
id = CLng (Trim (Request.QueryString ("id"))

As you can see, we call the trim () function every time, it is troublesome to repeat it repeatedly, and if we want to change the corresponding function in the future, we can use the filter parameter to write:
Copy Code code as follows:

'
' Function filter (key, value)
' Filter = Trim (value)
' End Function
'

Function filter (key, value)
On Error Resume Next

Select Case Key
Case "id" to determine if the ID is a number
If not IsNumeric (value) Then
Exit Function
End If

If CLng (value) <1 Then
Exit Function
End If
End Select

' Finally remember to let the function return the value, which in extract will be placed as the return value
Filter = Trim (value)

If err.number<>0 Then
Filter = ""
End If
End Function

Dim Query
Set query = Extract (Request.QueryString, "Name,id,job", "", "filter")

Response.Write Query.name
Response.Write Query.job
Response.Write Query.id

Set query = Nothing


Just now we are taking request.querystring as an example, of course you can also use Request.Form to achieve more functions of form processing, I hope this article can give you the convenience of writing ASP:-)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.