delphi字符串提取算法  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi字符串提取算法


private

FPageString:string;
FCurDir :string;
FHost :string;


function Tform1.GetLinks(Links: TStrings): integer;
var iLen, i: integer;
sLink: string;
boHref, boImg, boStart, boOther :Boolean;
ch :char;
p :PChar;
s :array[0..255] of char;
label addchEnd;
begin

result := -1;
Links.Clear;

iLen := length (FPageString);

boHref := False;
boImg := False;
boStart := False;
boOther := False;

sLink := '';

p := PChar (FPageString);

i := -1;

While True do begin
Inc (i);
if i >= iLen - 1 then break;
ch := p[i];
if boOther then begin
if ch = '>' then begin
boOther := False;
end ;
Continue;
end;

if (not boHref) and (not boImg) then begin
if i + 10 >= iLen then break;
if p[i] <> '<' then
continue;

if p[i+1] in ['A', 'a'] then begin
if p [i+2] = ' ' then begin
boHref := True;
boStart := False;
sLink := '';
Inc (i, 2);
Continue;
end;
end else

if p[i+1] in ['I', 'i'] then begin
StrLCopy ( @s, @p[i], 5);
s[5] := #0;

if SameText (s, 'boImg := True;
boStart := False;
sLink := '';
Inc (i, 4);
Continue;
end;
end;

boOther := True;
continue;
end;

if ch = '>' then begin
goto addchEnd;
end;

if not BoStart then begin
if boHref then begin
StrLCopy ( @s, @p[i], 5);
s[5] := #0;
if SameText (s, 'href=') then begin
boStart := True;
Inc (i, 4);

end;
end;

if boImg then begin
StrLCopy ( @s, @p[i], 4);
s[4] := #0;
if SameText (s, 'src=') then begin
boStart := True;
Inc (i, 3);
end;
end;

continue;
end;

if ch in [' ', '"', #39] then begin
if sLink <> '' then begin

goto addchEnd;
end;
Continue;
end;

if (ch = '.') and (p[i+1] = '/') then begin
Inc (i);
continue;
end;

sLink := sLink + ch;
continue;

addchEnd:
boHref := False;
boImg := False;
boStart := False;

if sLink <> '' then begin
if not SameText ('http://', Copy (sLink, 1, 7)) then
begin
if sLink[1] <> '/' then
sLink := FCurDir + sLink
else
sLink := FHost + sLink;
end;
Links.Add(sLink)
end;

end;

result := Links.Count;
end;



推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.14417099952698 seconds