[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: