我致力于从互联网下载和修改文本文件的程序。 这是该文件的一行:
55,-63-K%C3%BAsky+m%C5%88a,533,557,754625,10160,0
我想将此行修改为:
55,-63-Kúsky mňa,533,557,754625,10160,0
这意味着我想将%C3%BA
等文字片段修改为ú
字母,atc ......
我写了这个单位:
unit Moj_PHP_Text;
interface
uses SysUtils;
const
MAX_CH = 37;
tab1:array[1..MAX_CH] of string=('%C3%A1','%C3%81','%C3%A4','%C4%8D','%C4%8C',
'%C4%8E','%C3%A9','%C4%9B','%C3%AD','%C3%8D',
'%C4%BE','%C5%99','%C5%88','%C5%A1','%C5%A5',
'%C3%BA','%C3%9A','%C3%BD','%C5%AF','%C5%BE',
'%C5%BD','%C5%98','%C5%A0','%C3%B4','%C5%A4',
'%C4%8F','%C3%9D','%C3%89','%C3%B3','%C3%A0',
'%C5%9A','%C4%BD','%C3%BC','%C5%87','%C5%8D',
'%C3%96','%C3%B6');
tab2:array[1..MAX_CH] of char=('á','Á','ä','è','È',
'Ï','é','ì','í','Í',
'¾','ø','ò','š','',
'ú','Ú','ý','ù','ž',
'Ž','Ø','Š','ô','',
'ï','Ý','É','ó','a',
'Œ','¼','ü','Ò','o',
'Ö','ö');
function DecodePHPText(st1: string): string;
implementation
function DecodePHPText(st1: string): string;
var i,j:integer;
st,stt:string;
ch:char;
jj,male:boolean;
znak:byte;
begin
st:=''; stt:=''; jj:=true; male:=false; j:=0;
for i:=1 to length(st1) do begin
if jj then begin
ch:=st1[i];
if ch='+' then ch:=' ';
if (ch='%') then begin
stt:=copy(st1,i,3);
if not(stt='%C3') and not(stt='%C4') and not(stt='%C5') then begin
stt:=copy(stt,2,2);
stt:='$'+stt;
znak:=strtoint(stt);
ch:=chr(znak);
male:=true;
jj:=false;
end else begin
stt:=copy(st1,i,6);
for j:=1 to MAX_CH do
if tab1[j]=stt then begin
ch:=tab2[j];
jj:=false;
male:=false;
end;
end;
end;
j:=i;
st:=st+ch;
end;
if ((i=j+2) and male) or ((i=j+5) and not male) then begin
jj:=true;
j:=0;
end;
end;
result:=st;
end;
end.
在D7中有没有将文本%C3%A1
转换为char á
的例程?
或者我必须继续填充数组Tab1和Tab2?
答案 0 :(得分:2)
这是该功能:
function DecodePHPText(const Data:string):string;
var t:AnsiString;
p,q,l:integer;
b:byte;
begin
l:=Length(Data);
SetLength(t,l);
q:=1;
p:=1;
while (p<=l) do begin
case char(Data[p]) of
'+':t[q]:=' ';
'%': begin
inc(p);
b:=0;
case char(Data[p]) of
'0'..'9':inc(b,byte(Data[p]) and $F);
'A'..'F','a'..'f':inc(b,(byte(Data[p]) and $F)+9);
end;
inc(p);
b:=b shl 4;
case char(Data[p]) of
'0'..'9':inc(b,byte(Data[p]) and $F);
'A'..'F','a'..'f':inc(b,(byte(Data[p]) and $F)+9);
end;
t[q]:=AnsiChar(b);
end else t[q]:=Data[p];
end;
inc(p);
inc(q);
end;
SetLength(t,q-1);
Result:=UTF8Decode(t);
if (q>1) and (Result='') then Result:=t;
end;