Home >Backend Development >PHP Tutorial >[QMacros] 教程: 使用 QMacros 从 PHP 脚本中获取使用的变量名称列表
【注】本教程仅出于演示目的,并不代表作者推荐或者反对某种做法,也不代表这些代码有任何其它实际用途。
QMacros 的是一个高效的栈式查找替换引擎,为您提供丰富的功能。下面的代码演示了如何从一段 PHP+HTML 混合代码得到所有的 PHP 定义的变量名(其中源代码放到了Memo1 里,输出的变量名称直接放到了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, '<?php', '?>'); 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, '<?php', '?>'); 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;
把界面贴出来给大家便于对照着看:
好了,我们在 Button1 的 OnClick 事件里响应函数 Button1Click 里做了这么点事:
注意一点,我们在 OnMacroMissed 事件的处理中,都入栈了 AName 命名的参数,并设置 AHandled 为 True,以避免 QMacros 后面的默认处理代码。
好了,代码的简单解读就到这里,实际上很一目了然。这里看一下效果: