It's a function that receives two strings and returns another string. I'll include variable declarations in comments as they're introduced in the code; put them at the top of the function.
function convert(str: AnsiString; const key: AnsiString = ''): AnsiString;
If the key is empty, then the result is simply str:
begin
if key = '' then
Exit(str);
The first parameter is the value to be "encrypted," and the second is the key to use for that encryption. The key needs to be at least eight non-space characters long; anything beyond 32 is ignored. If the key is too short, the PHP script would terminate; we'll use Delphi's Assert statement instead since it's clear that the code should never even have executed if the key is wrong. (Script-termination is not a recoverable error that the user would be expected to fix.) The PHP code uses the ?: operator to select the desired value for the length, but Delphi's Min function (from the Math unit) expresses the desire more clearly.
// var ky: AnsiString;
ky := StringReplace(key, ' ', '', [rfReplaceAll]);
Assert(Length(ky) >= 8, 'key error');
// var kl: Integer;
kl := Min(Length(ky), 32);
The array k is used to hold numbers representing the lower five bits of each character in the key. In PHP, an array will automatically grow to whatever size it needs based on the index used. In Delphi, we need to allocate the space in advance. Since it's set in a loop that goes over each character of the key, we know the array will be the same length.
// var k: array of Byte;
SetLength(k, kl);
// var i: Integer;
for i := 0 to Pred(kl) do
k[i] := Ord(ky[i+1]) and $1f;
Next, each character in the string that has its seventh bit set gets modified according to each successive byte in the k array. The j variable keeps track of which key byte we'll use next.
// var j: Integer;
j := 0;
for i := 1 to Length(str) do begin
// var e: Byte;
e := Ord(str[i]);
if (e and $e0) <> 0 then
str[i] := AnsiChar(e xor k[j]);
Inc(j);
if j = kl then
j := 0;
// The previous three lines can also be written j := (j + 1) mod kl
end;
Finally, we return the new value of str:
Result := str;
end;