[QMacros] Tutorial: use QMacros to obtain the list of variables used in the PHP script

Source: Internet
Author: User
[QMacros] Tutorial: use QMacros to obtain the list of variables used in the PHP script [note] this tutorial is only for demonstration purposes and does not represent the author's recommendation or opposition to some practices, it does not mean that the code has any other practical use.

QMacros is an efficient stack search and replacement engine that provides you with a wide range of functions. The following code demonstrates how to get all PHP-defined variable names from a PHP + HTML hybrid code (the source code is put in Memo1, and the output variable name is directly put in 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: 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;
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;

Paste the interface for your reference:

Well, we have done this in the response function Button1Click in the OnClick event of Button1:

  • The instance AMacros of the TMacroManager object is created, and then the OnMacroMissed event response function is set to point to the DoCodeBlockFound function to locate The ending code block. The DoCodeBlockFound function is further processed:
    • An instance of a TQMacroManager object is created, and the OnMacroMissed event response function is set to point to DoVarNameFound. In DoVarNameFound, We output the variable name to Memo2.
  • Call the Complie method of AMacros to parse the source code and find the PHP code block. because we do not have a macro definition of any code block on the stack, it will trigger the DoCodeBlockFound function of the OnMacroMissed event, leading to further processing, complete the input task of variable name.

Note that in OnMacroMissed event processing, the AName parameter is added to the stack and AHandled is set to True to avoid the default code processing after QMacros.

Well, the simple explanation of the code is here, which is actually quite clear. Here, let's take 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.