[Qmacros] Tutorial: Using Qmacros to get a list of variable names used from PHP scripts

Source: Internet
Author: User
Note: This tutorial is for illustrative purposes only and does not imply that the author recommends or opposes a practice, nor does it imply any other practical use of the code.

Qmacros is an efficient, stack-finding and replacement engine that provides you with rich functionality. The following code shows how to get all the PHP-defined variable names from a section of php+html mixed code (where the source code is placed in the Memo1, and the output variable name is placed directly into the Memo2):

Delphi/pascal

Type TFORM1 = Class (Tform) Button1:tbutton;    Memo1:tmemo;    Memo2:tmemo;  Procedure Button1Click (Sender:tobject); Private {Private Declarations} procedure Docodeblockfound (Asender:tqmacromanager;      Aname:qstringw; Const AQUOTER:QCHARW;    var ahandled:boolean); Procedure Dovarnamefound (Asender:tqmacromanager;      Aname:qstringw; Const AQUOTER:QCHARW;  var ahandled:boolean); Public {public declarations} End;var form1:tform1;implementation{$R *.dfm}procedure Tform1.button1click (sender:to  bject); var Amacros:tqmacromanager;  Aresult:tqmacrocomplied;begin Amacros: = tqmacromanager.create;    Try alist.duplicates: = Dupignore;    amacros.onmacromissed: = Docodeblockfound; Complie is enough aresult: = Amacros.complie (Memo1.text, '
 ');      If Assigned (Aresult) then//we don ' t use the result for replace,so free it.  Freeandnil (Aresult);  Finally Amacros.free; End;end;procedure Tform1.docodeblockfound (Asender:tqmacromanager;  Aname:qstringw; Const AQUOTER:QCHARW;  var ahandled:boolean); var Avarparser:tqmacromanager;  Areplace:tqmacrocomplied;begin Avarparser: = tqmacromanager.create;    Try avarparser.onmacromissed: = Dovarnamefound;    Areplace: = Avarparser.complie (AName, ' $ ', ', ', Mrf_end_with_invalid_char);      If areplace <> nil then begin Asender.push (AName, ");      ahandled: = True;    Freeandnil (Areplace);  End  Finally Freeandnil (Avarparser); End;end;procedure Tform1.dovarnamefound (Asender:tqmacromanager;  Aname:qstringw; Const AQUOTER:QCHARW;  var ahandled:boolean); Begin MEMO2.LINES.ADD (AName);  Asender.push (AName, "); ahandled: = True;end;
Type TFORM1 = Class (Tform) Button1:tbutton;    Memo1:tmemo;    Memo2:tmemo;  Procedure Button1Click (Sender:tobject); Private {Private Declarations} procedure Docodeblockfound (Asender:tqmacromanager;      Aname:qstringw; Const AQUOTER:QCHARW;    var ahandled:boolean); Procedure Dovarnamefound (Asender:tqmacromanager;      Aname:qstringw; Const AQUOTER:QCHARW;  var ahandled:boolean); Public {public declarations} end; var Form1:tform1;  Implementation {$R *.DFM} procedure Tform1.button1click (sender:tobject); var Amacros:tqmacromanager;  Aresult:tqmacrocomplied;begin Amacros: = tqmacromanager.create;    Try alist.duplicates: = Dupignore;    amacros.onmacromissed: = Docodeblockfound; Complie is enough aresult: = Amacros.complie (Memo1.text, '
 ');      If Assigned (Aresult) then//we don ' t use the result for replace,so free it.  Freeandnil (Aresult);  Finally Amacros.free; End;end; Procedure Tform1.docodeblockfound (Asender:tqmacromanager;  Aname:qstringw; Const AQUOTER:QCHARW;  var ahandled:boolean); var Avarparser:tqmacromanager;  Areplace:tqmacrocomplied;begin Avarparser: = tqmacromanager.create;    Try avarparser.onmacromissed: = Dovarnamefound;    Areplace: = Avarparser.complie (AName, ' $ ', ', ', Mrf_end_with_invalid_char);      If areplace <> nil then begin Asender.push (AName, ");      ahandled: = True;    Freeandnil (Areplace);  End  Finally Freeandnil (Avarparser); End;end; Procedure Tform1.dovarnamefound (Asender:tqmacromanager;  Aname:qstringw; Const AQUOTER:QCHARW;  var ahandled:boolean); Begin MEMO2.LINES.ADD (AName);  Asender.push (AName, "); ahandled: = True;end;

The interface is pasted out for everyone to look at the comparison:

Well, we did this in response function Button1Click in Button1 's OnClick event:

    • You create an instance of the Tmacromanager object Amacros, and then set its Onmacromissed event response function to the Docodeblockfound function, which is used to locate the block of code to end. The Docodeblockfound function has been further processed:
      • An instance of the Tqmacromanager object is created, and then the response function that sets its onmacromissed event points to Dovarnamefound. In Dovarnamefound, we will output the variable name we found in the Memo2.
    • Call Amacros's Complie method, parse the source code, find the PHP code block, because we do not stack any code block macro definition, so it will trigger the Onmacromissed event response function Docodeblockfound, causing further processing, Complete the input task for the variable name.

Note that, in the handling of the onmacromissed event, we stack the AName named parameters and set ahandled to True to avoid the default processing code behind the Qmacros.

Well, the simple interpretation of the code is here, and it's actually pretty straightforward. Here's a look at the effect:

  • 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.