2012-02-21 10 views
1

私はPostgreSQLのHSTOREデータ型とPHPを扱っています。PostgreSQLのHSTOREをPHPの配列に変換する

私はPHPの配列にPostgreSQLの返さhstoreの文字列を変換する機能を必要とします。私はで述べたように、私は値を保存し、私は次のような値に遭遇したとき、それを取得しようとただしたときfromPg()メソッドは、[2] [この記事を] hereを定義して使用してみました:

"myKey" => "my\"Value" 

... fromPg()メソッドは、値を2つの配列エントリに分割します。最初のキーはキー "myKey"、値は "my \"、2番目のキーは "値\" "とブランク誰がこの問題を抱えている場合、誰かがそれは同等だPHPの配列にhstoreの文字列を変換するより機能的な方法を提供することをいとわないならば値が。

私は思ったんだけど。

私は最終的に分解され、具体的に取り組むために設計された私自身のPHPライブラリを作成しました

<?php 
function fromPg($data) { 
    $split = preg_split('/[,\s]*"([^"]+)"[,\s]*|[,=>\s]+/', $data, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); 
    $hstore = array(); 
    for($index = 0; $index < count($split); $index = $index + 2) { 
     $hstore[$split[$index]] = $split[$index + 1] != 'NULL' ? $split[$index + 1] : null; 
    } 
    return $hstore; 
} 

$hstore_string = '"myKey" => "my\"Value"'; 
$hstore_array = fromPg($hstore_string); 

print '<pre>'; 
print_r($hstore_array); 
print '</pre>'; 

:ここに

は、私は、既存の「解決策」を抱えている問題を再現するコードサンプルですこの問題だけでなく、などそれはでGitHubの上で利用できる自動検出とPostgreSQL Hstoresの変換、アレイ、日付、幾何データ型などPHPのPostgreSQLのドライバの他の多くの欠点:https://github.com/JDBurnZ/PHPG

+0

可能な複製[postgresql hstoreをPHP配列に変換](http://stackoverflow.com/questions/6742563/convert-postgresql-hstore-to-php-array) – plaes

答えて

1

はこれを試してみてください。

function unescape_hstore($hstore) 
{   
    $hstore = preg_replace('/([$])/', "\\\\$1", $hstore); 
    $unescapedHStore = array(); 
    eval('$unescapedHStore = array(' . $hstore . ');'); 
    return $unescapedHStore; 
} 

そして、この関数をpostgresqlの出力に渡します。

0

Pomm's converter systemを使用できます。これは、PHP - PostgreSQLとの間でHSToreコンバータが付属しています。

は、ユーザーの入力から(直接またはしない)来るhstoreの文字列を解釈している場合は、ピョートルのソリューションは、(すべてのユーザの入力試用版として)潜在的に危険である

1

...それがお役に立てば幸いです。私はそれを乱用する方法を考え出すことはできませんが、未知の文字列を評価するのをやめてしまいます。

使用:

hstoreの(<some_array_or_object>、偽)リテラル有効hstoreの列に入力を変換し、それを返す:

hstore(array('k1' => 'v1', 'k2' => 'v2')) => "k1"=>"v1","k2"=>"v2" 

hstoreの(<some_array_or_object>)が有効に入力を変換しますhstore、続いて:: hstore

hstore(array('k1' => 'v1', 'k2' => 'v2')) => '"k1"=>"v1","k2"=>"v2"'::hstore 

hstoreの(<some_string>)は(それがクエリから来るように)アレイ

hstore('"k1"=>"v1","k2"=>"v2"') => array('k1' => 'v1', 'k2' => 'v2') 

それはNULL値(両方の方法)を処理するhstoreの列から変換し、適切にエスケープ/キーと値をアンエスケープ。

<?php 

/** 
* mixed hstore(mixed $input[, bool $prepared = false]) 
*  Convert from hstore string to array, or from array/object to hstore. 
* Inner arrays/objects are serialized but deserialization is up to you; you 
* are expected to keep track of which fields you sent as non-scalars. 
* 
* @param mixed $input   A string (from hstore) or an array/object 
* @param type $prepared  Array or object to convert to hstore string 
* @return mixed    Depends on the input 
*/ 
function hStore($input, $prepared=true) 
{ 
    if (is_string($input)) 
    { 
     if ($input === 'NULL') 
     { 
      $output = NULL; 
     } 
     else 
     { 
      $re = '_("|^)(.*?[^\\\\"])"=>"(.*?[^\\\\"])("|$)_s'; 
      preg_match_all($re, $input, $pairs); 
      $mid = $pairs ? array_combine($pairs[2], $pairs[3]) : array(); 

      foreach ($mid as $k => $v) 
      { 
       $output[trim($k, '"')] = stripslashes($v); 
      } 
     } 
    } 
    elseif (is_null($input)) 
    { 
     $output = $prepared ? 'NULL::hstore' : 'NULL'; 
    } 
    elseif (!is_scalar($input)) 
    { 
     foreach ((array)$input as $k => $v) 
     { 
      !is_scalar($v) && ($v = serialize($v)); 
      $entries[] = '"' . addslashes($k) . '"=>' . 
         '"' . addslashes($v) . '"'; 
     } 

     $mid = empty($entries) ? '' : join(', ', $entries); 

     $output = $prepared ? "'{$mid}'::hstore" : $mid; 
    } 

    return $output; 
} 

?> 

この質問は確かにConvert postgresql hstore to php arrayの複製です。

私はその投稿に私の答えを投稿し直しています。

関連する問題