Home > Backend Development > PHP Tutorial > [QMacros] 教程: 使用 QMacros 从 PHP 脚本中获取使用的变量名称列表

[QMacros] 教程: 使用 QMacros 从 PHP 脚本中获取使用的变量名称列表

WBOY
Release: 2016-06-23 13:14:14
Original
848 people have browsed it

【注】本教程仅出于演示目的,并不代表作者推荐或者反对某种做法,也不代表这些代码有任何其它实际用途。

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;
Copy after login
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;
Copy after login

把界面贴出来给大家便于对照着看:

好了,我们在 Button1 的 OnClick 事件里响应函数 Button1Click 里做了这么点事:

  • 创建了 TMacroManager 对象的实例 AMacros,然后设置其 OnMacroMissed 事件的响应函数,指向 DoCodeBlockFound 函数,用于找到以 结束的代码块。DoCodeBlockFound 函数做了进一步的处理:
    • 创建了一个 TQMacroManager 对象的实例,然后设置其 OnMacroMissed 事件的响应函数指向 DoVarNameFound。在 DoVarNameFound 里,我们将找到的变量名输出到Memo2中。
  • 调用 AMacros 的 Complie 方法,解析源代码,找到 PHP 代码块,由于我们没有入栈任何代码块的宏定义,所以它会触发 OnMacroMissed 事件的响应函数 DoCodeBlockFound,从而引发进一步的处理,完成变量名称的输入任务。

注意一点,我们在 OnMacroMissed 事件的处理中,都入栈了 AName 命名的参数,并设置 AHandled 为 True,以避免 QMacros 后面的默认处理代码。

好了,代码的简单解读就到这里,实际上很一目了然。这里看一下效果:

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template