note 105967 deleted from function.pg-escape-bytea by danbrown
| From: | danbrown@php.net | Date: | Fri, 30 Sep 2011 21:38:50 +0000 |
| Subject: | note 105967 deleted from function.pg-escape-bytea by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-183565@lists.php.net to get a copy of this message | ||
Note Submitter: zlobnynigga at yandex dot ru
----
If you want to use pg_escape_bytea and have no pgsql extension, or if you want old-style
escaping(see https://bugs.php.net/bug.php?id=53127 for details)
you can use
function pg_escape_bytea($str)
{
$raw = unpack('C*', $str);
$res = '';
foreach ($raw as $charcode) {
if ($charcode < 0x20 || $charcode > 0x7e) {
$res .= '\\\\' . ($charcode >> 6);
$res .= ($charcode >> 3) & 0x07;
$res .= $charcode & 0x07;
} elseif ($charcode === 0x27){
$res .= "''";
} elseif ($charcode === 0x5c) {
$res .= '\\\\\\\\';
} else {
$res .= chr($charcode);
}
}
return $res;
}
It is based on PQescapeByteaInternal function from PostgreSQL 9.0.4 sources. NB: this function
depends on 'standard_conforming_strings' setting of PostgreSQL, it must be set to off. If
you want to set it to on, halve escaping slashes number.