background image

PHP 代码:php 读取 Postgresql 中的数组

代码如下:
function getarray_postgresql($arraystr)
{
$regx1 = '/^{(.*)}$/';
$regx2 = "/\"((\\\\\\\\|\\\\\"|[^\"])+)\"|[^,]+/";
$regx3 = '/^[^"].*$|^"(.*)"$/';
$match = null;
preg_match( $regx1,$arraystr,$match);
$str = $match[1];
preg_match_all($regx2, $str,$match);
$items = $match[0];
$array = array();
$count = count($items);
for($index = 0; $index < $count;++$index)
{
preg_match($regx3, $items[$index],$match);
$array[$index]=end($match);
}
return $array;
}

在 PHP 从 postgresql 中 读 取 的 数 据 都 是 字 符 串 的 , 一 般 的 数 据 还 好 处 理 , 但 是
postgresql 有一种数组型的数据,而如果我们的数组是字符串的,前且,里面有逗号或
斜线也是可能的,这就给我们读取带来了一定的麻烦,上面的函数是我奋斗了几个小时
写出来的。尽可能的考虑到了斜线,逗号,引号的存在。