Variable name-value transformations are easily implemented in ASP

Source: Internet
Author: User


PHP friends know that the use of variables in PHP is flexible and convenient, 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 the same functionality in ASP must be written as: "Update useres set password= '" & Password & "', group=" & Group & ", Name= '" & username & "' Where Account= '" & Account & "'", looks lengthy and ugly. If this is an insert language and there are a lot of fields inserted, it would be a painful process to look at the correspondence between the fields and values.

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

Ideas

First, there must be a way to differentiate the variable names that need to be replaced by the actual values from the normal text, and then replace all the found variable names with the actual values that they represent.
For the 1th can be found through regular expressions, here we do not take the variable representation of PHP, and the use of the large bracket {} as the variable name of the boundary character, the string representation becomes password= ' {password} ', Group={group}.
The 2nd is the key of the variable name-value transformation, which gets the value of the variable by the variable name. View ASP data There is no direct implementation of the method, but there is a function execute to draw our attention, from the data note that execute can execute an incoming valid string as code execution, so long as the writing of a small function can be implemented by us 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

Full 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 is valid  
Case 1 Regex.pattern =" \{([\w+\-\*/\\<>=]+) \} "variable name and operator valid  
' case 2 Regex.pattern =" \ {([\w\s]+) \} "' All characters except line break are valid  
Case Else exit function 
end select 
' Regex.pattern = ' \{(\w+) \} "&NBSP;
Regex.ignorecase = true 
Regex.global = true 
Set Matches = Regex.execute (str)  
result = str 
' Response.Write matches.count 
for each Match in matches 
result = Replace (Result , Match.value, GetVar (match.submatches (0)))  
next 
Set Matches = nothing 
Set regEx = nothing& nbsp
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 out that all variable names are called GetVar to get the value of the variable and replace it. Actually calling Txt2value directly (str,1) also allows the string value to be arithmetic.

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.