PHP code to unescape data retrieved from a bytea column - Mailing list pgsql-general

From huongch@bigfoot.com (Flancer)
Subject PHP code to unescape data retrieved from a bytea column
Date
Msg-id 8c8c68a1.0110240118.441559a@posting.google.com
Whole thread Raw
Responses Re: PHP code to unescape data retrieved from a bytea column
List pgsql-general
To those who has been asking for it.. here it is.. pieced it togather
within a few hours so please excuse my clumsy syntaxes or algorithm or
my speed (for those who can actually code it a lot faster.... erm then
again I guess you won't be reading this post anyway).

function checkalphanum($ct,$i)
{
    //find if next 3 characters are digits or not
    for ($digitcount=0,$j = $i+1,$actual_count=1; ($actual_count <= 3) &&
($j < strlen($ct));++$actual_count, ++$j)
    {
        if ((ord($ct[$j]) >= 48) && (ord($ct[$j]) <= 57))
        {
            ++$digitcount;
        }
    }
    return $digitcount;
}

function unescape($ct,$i)
{
    $buff = "";
    for ($j = $i,$actual_count=1; ($actual_count <= 3) && ($j <
strlen($ct));++$actual_count, ++$j)
    {
             $buff .= $ct[$j];
    }
    return chr(octdec(intval($buff)));
}

function decode($ct)
{
 $buf = "";
 for ($i = 0; $i < strlen($ct); $i++)
 {
  if ($ct[$i] == "\\")
  {
   if ($ct[$i + 1] == "\\")
   {
     if (checkalphanum($ct,$i+1) == 3)
     {
         $i +=2;
         $buf .= unescape($ct,$i);
         $i +=2;
     }
     else
     {
         $buf .=$ct[$i];
     }
   }
   else
   {
    if (checkalphanum($ct,$i) == 3)
    {
        ++$i;
        $buf .= unescape($ct,$i);
        $i +=2;
    }
   }
  }
  else
  {
   $buf .= $ct[$i];
  }
 }
 return $buf;
}

I have tested it using data compressed using the bzcompress.. and
voila it bzdecompres perfectly using my function ;).
To use it just call something like

print decode($rawdatafrombytea);

So far I have only tested on data that was encoded using another PHP
function done by Joe Conway.

You can find that code here

http://groups.google.com/groups?q=bytea&hl=ms&group=comp.databases.postgresql.general&rnum=7&selm=027601c12c08%2454576a20%2448d210ac%40jecw2k1

If anyone out there can improve this code or already has a better
code.. please let me know ;).

pgsql-general by date:

Previous
From: jmsz@wespine.com.au (James Szabadics)
Date:
Subject: Installation problems
Next
From: qk22@gmx.co.uk (qk22)
Date:
Subject: Re: Can not input large text data