RSS订阅 | 匿名投稿
您的位置:网站首页 > 字符串 > 正文

delphi 过滤网页代码 <SCRIPT></SCRIPT>

作者:admin 来源: 日期:2018/3/13 21:46:30 人气: 标签:

uses StrUtils;

function ClearScript(AHtml: string): string;
const
  csHead = '<SCRIPT';
  csTail = '</SCRIPT>';
  cnHead = length(csHead);
  cnTail = length(csTail);
var
  nHeadPos: integer;
  nTailPos: integer;
  nNewSearchPos: integer;
  nHtmlLength: integer;
  sCapHtml: string;
  sFilteredHtml: string;
  nHtmlPos: integer;
  nSecSize: integer;

  function FindHeadPos: boolean;
  begin
    nHeadPos := PosEx('<SCRIPT', sCapHtml, nNewSearchPos);
    result := nHeadPos > 0;
  end;

  function FindTailPos: boolean;
  begin
    nTailPos := PosEx('</SCRIPT>', sCapHtml, nHeadPos + cnHead);
    result := nTailPos > 0;
  end;

begin

  nHtmlLength := length(AHtml);
  nNewSearchPos := 1;
  nHtmlPos := 1;
  sCapHtml := UpperCase(AHtml);

  while FindHeadPos do
  begin

    nSecSize := nHeadPos - nHtmlPos;
    if nSecSize > 0 then
    begin
      sFilteredHtml := sFilteredHtml + Copy(AHtml, nHtmlPos, nSecSize);
    end;

    nHtmlPos := nHeadPos + cnHead;

    if FindTailPos then
    begin
      nNewSearchPos := nTailPos + cnTail;
      nHtmlPos := nTailPos + cnTail;
    end
    else
      break;

  end;

  nSecSize := nHtmlLength - nHtmlPos + 1;

  if nSecSize > 0 then
  begin
    sFilteredHtml := sFilteredHtml + Copy(AHtml, nHtmlPos, nSecSize);
  end;
  result := sFilteredHtml;
end;


procedure TForm2.Button1Click(Sender: TObject);
begin
memo2.Text:=ClearScript(memo1.Text);
end;

读完这篇文章后,您心情如何?
0
0
0
0
0
0
0
0
本文网址:
下一篇:没有资料