Delphi URLEncode like PHP

Posted on September 12, 2020 at 11:53 am

Example URLEncode() function converted from PHP to Delphi:

function URLEncode(const aData: AnsiString): AnsiString;
const
      Chars: Array [0..18] of AnsiChar = (#$25, #$21, #$2A, #$27, #$28, #$29,
                                          #$3B, #$3A, #$40, #$26, #$3D, #$2B,
                                          #$24, #$2C, #$2F, #$3F, #$23, #$5B,
                                          #$5D);
var
     sData: String;
     i1: DWORD;
begin
     sData := String(aData);
     for i1 := Low(Chars) to High(Chars) do
     sData := StringReplace(sData, String(Chars[i1]), '%' + IntToHex(Ord(Chars[i1]), 2), [rfReplaceAll, rfIgnoreCase]);
     result := AnsiString(sData);
end;

Receive updates via email

Other Posts

Updated Posts