note 89841 modified in ref.pgsql by danbrown

From: Date: Wed, 25 Mar 2009 17:14:54 +0000
Subject: note 89841 modified in ref.pgsql by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-152435@lists.php.net to get a copy of this message
Parsing a postgres array can be a tricky problem, he's my take on this, it handles multi-dimensional arrays plus escaping using a nasty regexp to determine the limits of each data-item. <?php function pg_array_parse( $text, &$output, $limit = false, $offset = 1 ) { if( false === $limit ) { $limit = strlen( $text )-1; $output = array(); } if( '{}' != $text ) do { if( '{' != $text{$offset} ) { preg_match( "/(\\{?\"([^\"\\\\]|\\\\.)*\"|[^,{}]+)+([,}]+)/", $text, $match, 0, $offset ); $offset += strlen( $match[0] ); $output[] = ( '"' != $match[1]{0} ? $match[1] : stripcslashes( substr( $match[1], 1, -1 ) ) ); if( '},' == $match[3] ) return $offset; } else $offset = pg_array_parse( $text, $output[], $limit, $offset+1 ); } while( $limit > $offset ); return $output; } ?> --was-- Parsing a postgres array can be a tricky problem, he's my take on this, it handles multi-dimensional arrays plus escaping using a nasty regexp to determine the limits of each data-item. function pg_array_parse( $text, &$output, $limit = false, $offset = 1 ) { if( false === $limit ) { $limit = strlen( $text )-1; $output = array(); } if( '{}' != $text ) do { if( '{' != $text{$offset} ) { preg_match( "/(\\{?\"([^\"\\\\]|\\\\.)*\"|[^,{}]+)+([,}]+)/", $text, $match, 0, $offset ); $offset += strlen( $match[0] ); $output[] = ( '"' != $match[1]{0} ? $match[1] : stripcslashes( substr( $match[1], 1, -1 ) ) ); if( '},' == $match[3] ) return $offset; } else $offset = pg_array_parse( $text, $output[], $limit, $offset+1 ); } while( $limit > $offset ); return $output; } http://php.net/manual/en/ref.pgsql.php

« previous php.notes (#152435) next »