Friends who have used PHP know that the use of variables in PHP is flexible and convenient, especially the variable name-value conversion in strings can be easily realized, making the entire PHP code more concise and beautiful. For example, a SQL statement to update the database only needs to be written as:
program code
"update users set password='$password', group=$group, name='$username' where account='$account'"
, $password, $group, $username, $account will be replaced by the actual variable values. To achieve the same function in ASP, it must be written as:
program code
"update useres set password='" & password & "',group=" & group & ",name='" & username & "' where account='" & account & "'"
, seems lengthy and ugly. If this is an insert language and there are a lot of inserted fields, checking the correspondence between fields and values will be a painful process.
Now let's see how to implement similar variable name-value transformation in ASP.
The idea
is that first, there must be a way to distinguish the variable names that need to be replaced with actual values from ordinary text; then, replace all found variable names with the actual values they represent.
The first point can be found through regular expressions. Here we do not use PHP's variable representation, but use the big number {} as the boundary character of the variable name. The string representation becomes password='{password}',group ={group}.
The second point is the key to variable name-value conversion. The variable value is obtained through the variable name. Looking at the ASP information, we did not find a direct implementation method, but there is a function Execute that caught our attention. From the information description, we know that Execute can execute the incoming valid string as a code execution synchronization. In this way, we only need to write a small function to achieve our goal. instructions. The core code is:
program code
function GetVar(var_name)
Execute("function get_value(): get_value=" & var_name & ": end function")
getvar=get_value()
end function
Implement
the complete code:
program
code'DESIGN BY: Peng Guohui
'SITE: http://kacarton.yeah.net/
'BLOG: http://blog.csdn.net/nhconch
'EMAIL: [email protected]
'The article is original to the author. Please contact me before reprinting. Please indicate the source of the article and retain the author's information. Thank you for your support!
'================================================== ================
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()]+)}" 'The variable name is valid
case 1 regEx.Pattern = "{([w+-*/\<>=()]+)}" 'Variable names and operators are valid
'case 2 regEx.Pattern = "{([ws()]+)}" 'All characters except newline characters are valid
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 Each 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
Calling method:
program code
Var2Value("update users set password='{password}', group={group}, name='{username}' where account='{account}'"
Var2Value calls Txt2Value, Txt2Value finds all variable names and calls GetVar Get the variable value and replace it. In fact, calling Txt2Value(str,1) directly also allows four arithmetic operations on the string value.