Easily implement variable name - value transform in ASP

zhaozj2021-02-12  165

Friends who have used PHP know that the use of variables in PHP is flexible, especially in the string, which makes it easy to implement variable name-value transform, making the entire PHP code more simple and beautiful. For example, a new SQL statement for updating the database is only written: "Update users set password = '$ password', group = $ group, name = '$ usrname' where account = '$ account'", where $ Password, $ group, $ UserName, $ Account will be replaced by actual variable value, and in the ASP to implement the same function must be written: "Update useres set password = '" & password & ", group =" & group & ", name = '"& username &"' where account = '"& account", "is very ugly. If this is a insert language and inserting a lot of content, then the corresponding relationship with the VALUES will be a painful process.

Let us now look at how to achieve a similar variable name-value transform in ASP.

Think

First, there must be a method to distinguish between variable names that need to be replaced with actual values; then, then replace all found variable names as the actual value it represents. For the first point, it can be found by the regular expression. Here we do not use the PHP variable representation, and the larger key {} is used as the boundary character of the variable name, the string represents into password = '{Password}', group = {Group}. The second point is the key to the variable name-value transformation, and the variable value is obtained by the variable name. Viewing the ASP data does not find a direct implementation, but there is a function Execute caused us to pay attention. From the information description, Execute can perform incoming valid strings as the code execution, so as long as we write a small function to implement us The presentation. The core code is: function getvar ("Function get_value (): get_value =" & var_name & "end function" getvar = get_value () end function

achieve

Complete code: '============================================== =================== 'Design By: Peng Guowei' site: http://kacarton.yah.net/'blog: http://blog.9cbs.net/ NHCONCH'EMAIL: Kacarton@sohu.com 'The article is the author original. Please contact him before reprinting, please indicate the article, retain the author information, thank you for your support! '==================================================== ================ Function getvar ("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 the line breaks Vase 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 = Resultend functionfunction Var2Value (var_name) Var2value = txt2value (var_name, 0) End Function

Calling method: var2value ("Update users set password = '{password}', group = {group}, name = '{username}' where account = '{account}'" var2value calls TXT2VALUE, TXT2VALUE to find all variable names The transfer calls GetVar gets the variable value and replaces it. In fact, TXT2VALUE (STR, 1) also allows four arithmetic operations to string values.

转载请注明原文地址:https://www.9cbs.com/read-6686.html

New Post(0)