Easy implementation of variable name-value transformation in ASP

Source: Internet
Author: User
Tags php code valid

PHP friends know that the use of flexible PHP variables, especially in the string to facilitate the implementation of variable name-value transformation, making the entire PHP code more concise and beautiful. For example, an SQL statement that updates a database simply writes: "Update users set password= ' $password ', group= $group, name= ' $username ' where account= ' $account '", The $password, $group, $username, $account will be replaced by the actual variable values, and in the ASP to achieve the same functionality must be written: "Update useres set password= '" & Password & "', Group= & group &", Name= ' & username & ' where account= ' "& Account &" ", Looks long and ugly. If this is an insert language and there are a lot of inserted fields, it would be a painful process to look at the correspondence between the fields and values.

Now let's see how to implement a similar variable name-value transformation in ASP.

Ideas

First, there must be a way to distinguish between variable names that need to be replaced by actual values and ordinary text, and then replace all the names of the variables found with the actual values they represent.

For the 1th can be found through regular expressions, where we do not use the variable representation of PHP, and the use of the large bracket {} as the variable name of the boundary character, the string representation into password= ' {password} ', Group={group}.

The 2nd is the key of variable name-value transformation, and the variable value is obtained by variable name. View the ASP data did not find a direct implementation method, but there is a function execute caught our attention, from the data description, we know that execute can execute the incoming valid string as code execution, so as long as the writing a small function can implement our want to show. The core code is:

function GetVar(var_name)
    Execute("function get_value(): get_value=" & var_name  & ": end function")
    getvar=get_value()
end function

Realize

Complete code:

function GetVar (var_name)
Execute ("Function get_value (): get_value=" & Var_name & ": End Function")
Getvar=get_value ()
End Function

function Txt2value (str, level)
Dim regEx, Matches, result
Set regEx = new REGEXP
Select Case Level
Case 0 Regex.pattern = "{(w+)}" ' Variable name valid
Case 1 Regex.pattern = "{([w+-*/\<>=]+)}" ' Variable name and operator valid
' Case 2 Regex.pattern = ' {([ws]+)} ' ' is valid for all characters except line breaks
Case Else Exit function
End Select
' Regex.pattern = ' {(w+)} '
Regex.ignorecase = True
Regex.global = True
Set matches = Regex.execute (str)
result = Str
' Response.Write Matches.count
For the Match in matches
result = Replace (result, Match.value, GetVar (match.submatches (0)))
Next
Set matches = Nothing
Set regEx = Nothing
Txt2value = result
End Function

function Var2value (var_name)
Var2value = Txt2value (var_name, 0)
End Function

Call Method:

Var2Value("update users set password='{password}', group={group}, name='{username}' where account='{account}'"

Var2value called Txt2value,txt2value to find all the variable name calls GetVar get the variable value and replace it. Actually calling Txt2value (str,1) directly also allows arithmetic of string values.

Related Article

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.