Easily implement variable name-value transformation in ASP

Source: Internet
Author: User

Anyone who has used PHP knows that the use of variables in PHP is flexible and convenient, especially in strings.Implement variable name-value transformationMakes the entire PHP code more concise and elegant. For example, you only need to write an SQL statement for updating a database as follows: "Update users SET Password = '$ password', group = $ group, name = '$ username' where account =' $ Account' ", where $ password, $ group, $ username, and $ account are replaced by the actual variable values, to implement the same functions in ASP, you must write: "Update useres SET Password = '" & password & "', group =" & Group &", name = '"& username &" 'where account =' "& account &" '", which is lengthy and ugly. If this is an insert language with a lot of inserted fields, it will be a painful process to view the correspondence between the field and values.

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

Ideas

First, you must have a method to replaceVariable names are differentiated from common texts.; Then, put all the variable names foundReplace with the actual value it representsDrop.
The first point can be found through a regular expression. Here we do not use the PHP variable representation, but use the large token {} as the variable name boundary, string to Password = '{password}', group = {group }.
The second point is the key to variable name-value transformation. The variable value is obtained through the variable name. No direct implementation method was found for viewing ASP data, but there is a functionExecuteNote that execute can execute the input valid string as the code execution, so that we can implement the display as long as a small function is written. The core code is:
FunctionGetvar (var_name)
Execute ("function get_value (): get_value =" & var_name & ": end function ")
Getvar = get_value ()
End Function

Implementation

Complete code:
'================================================ ======================================
'Design by: Peng Guohui
'Site: http://kacarton.yeah.net/
'Blog: http://blog.csdn.net/nhconch
'Email: kacarton@sohu.com

'The article is original to the author. Please contact me before reprinting. Reprinting should indicate the source of the article and retain the author information. Thank you for your support!
'================================================ ======================================

FunctionGetvar (var_name)
Execute ("function get_value (): get_value =" & var_name & ": end function ")
Getvar = get_value ()
End Function

FunctionTxt2value (STR, level)
DimRegEx, matches, result
SetRegEx =NewRegexp
Select CaseLevel
Case0 RegEx. pattern = "/{(/W +)/}" 'variable name valid
Case1 RegEx. pattern = "/{([/W +/-/* // <> =] +)/}" 'variable name and operator are valid
'Case 2 RegEx. pattern = "/{([/W/S] +)/}" 'All characters except line breaks are valid
Case else exit function
End select
'Regex. pattern = "/{(/W + )/}"
RegEx. ignorecase =True
RegEx. Global =True
SetMatches = RegEx. Execute (STR)
Result = Str
'Response. Write matches. Count
For eachMatchInMatches
Result = Replace (result, match. Value, getvar (match. submatches (0 )))
Next
SetMatches =Nothing
SetRegEx =Nothing
Txt2value = Result
End Function

FunctionVar2value (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 calls txt2value, and txt2value finds all variable names and calls getvar to obtain and replace the variable value. In fact, directly calling txt2value (STR, 1) also allows four arithmetic operations on 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.