1

我正在研究从互联网下载和修改文本文件的程序。这是该文件的一行:

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%BAletter ú, 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为字符的例程?á或者我必须继续填充数组 Tab1 和 Tab2?

4

1 回答 1

2

好的,问题解决了。我在某个地方找到了我需要的东西。这是一个功能。我修改了这个功能,现在它完美地工作了。感谢 kbec 来写,我必须寻找的东西...... ;-)

这是该功能:

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;
于 2013-02-23T20:00:48.423 回答