簡體   English   中英

PHP:字符串到多維數組

[英]PHP: String to multidimensional array

(對不起,我的英語不好)

我有一個字符串,我想拆分成一個數組。 角括號是多個嵌套數組。 應保留轉義字符。

這是一個示例字符串:

$string = '[[["Hello, \"how\" are you?","Good!",,,123]],,"ok"]'

結果結構應如下所示:

array (
  0 => 
  array (
    0 => 
    array (
      0 => 'Hello, \"how\" are you?',
      1 => 'Good!',
      2 => '',
      3 => '',
      4 => '123',
    ),
  ),
  1 => '',
  2 => 'ok',
)

我測試過它:

$pattern = '/[^"\\]*(?:\\.[^"\\]*)*/s';
$return = preg_match_all($pattern, $string, null);

但這不能正常工作。 我不明白這些RegEx模式(我在本頁的另一個例子中發現了這一點)。 我不知道preg_match_all是否是正確的命令。

我希望有一個人可以幫助我。

非常感謝!!!

對於正則表達式來說這是一個艱難的 - 但是你的問題有一個黑客的答案(提前道歉)。

字符串幾乎是一個有效的數組字面但對於,,秒。 您可以匹配這些對,然后轉換為,''

/,(?=,)/

然后,您可以將該字符串eval到您要查找的輸出數組中。

例如:

// input 
$str1 = '[[["Hello, \\"how\\" are you?","Good!",,,123]],,"ok"]';

// replace , followed by , with ,'' with a regex
$pattern = '/,(?=,)/';
$replace = ",''";
$str2 = preg_replace($pattern, $replace, $str1);

// eval updated string
$arr = eval("return $str2;");
var_dump($arr);

我明白了:

array(3) {
  [0]=>
  array(1) {
    [0]=>
    array(5) {
      [0]=>
      string(21) "Hello, "how" are you?"
      [1]=>
      string(5) "Good!"
      [2]=>
      string(0) ""
      [3]=>
      string(0) ""
      [4]=>
      int(123)
    }
  }
  [1]=>
  string(0) ""
  [2]=>
  string(2) "ok"
}

編輯

注意到eval的固有危險,更好的選擇是使用json_decode和上面的代碼,例如:

// input 
$str1 = '[[["Hello, \\"how\\" are you?","Good!",,,123]],,"ok"]';

// replace , followed by , with ,'' with a regex
$pattern = '/,(?=,)/';
$replace = ',""';
$str2 = preg_replace($pattern, $replace, $str1);

// eval updated string
$arr = json_decode($str2);
var_dump($arr);

如果您可以編輯序列化數據的代碼,那么最好讓使用json_encode和json_decode處理序列化。 無需在這一個上重新發明輪子。

好貓順便說一下。

您可能希望將詞法分析器與實際構建結構的遞歸函數結合使用。

出於您的目的,使用了以下令牌:

\[           # opening bracket
\]           # closing bracket
".+?(?<!\\)" # " to ", making sure it's not escaped
,(?!,)       # a comma, not followed by a comma
\d+          # at least one digit
,(?=,)       # a comma followed by a comma

其余的是編程邏輯,請參閱ideone.com上演示 靈感來自這篇文章


class Lexer {
    protected static $_terminals = array(
        '~^(\[)~'               => "T_OPEN",
        '~^(\])~'               => "T_CLOSE",
        '~^(".+?(?<!\\\\)")~'   => "T_ITEM",
        '~^(,)(?!,)~'           => "T_SEPARATOR",
        '~^(\d+)~'              => "T_NUMBER",
        '~^(,)(?=,)~'           => "T_EMPTY"
    );

    public static function run($line) {
        $tokens = array();
        $offset = 0;
        while($offset < strlen($line)) {
            $result = static::_match($line, $offset);
            if($result === false) {
                throw new Exception("Unable to parse line " . ($line+1) . ".");
            }
            $tokens[] = $result;
            $offset += strlen($result['match']);
        }
        return static::_generate($tokens);
    }

    protected static function _match($line, $offset) {
        $string = substr($line, $offset);

        foreach(static::$_terminals as $pattern => $name) {
            if(preg_match($pattern, $string, $matches)) {
                return array(
                    'match' => $matches[1],
                    'token' => $name
                );
            }
        }
        return false;
    }

    // a recursive function to actually build the structure
    protected static function _generate($arr=array(), $idx=0) {
        $output = array();
        $current = 0;
        for($i=$idx;$i<count($arr);$i++) {
            $type = $arr[$i]["token"];
            $element = $arr[$i]["match"];
            switch ($type) {
                case 'T_OPEN':
                    list($out, $index) = static::_generate($arr, $i+1);
                    $output[] = $out;
                    $i = $index;
                    break;
                case 'T_CLOSE':
                    return array($output, $i);
                    break;
                case 'T_ITEM':
                case 'T_NUMBER':
                    $output[] = $element;
                    break;
                case 'T_EMPTY':
                    $output[] = "";
                    break;
            }
        }
        return $output;
    }    
}

$input  = '[[["Hello, \"how\" are you?","Good!",,,123]],,"ok"]';
$items = Lexer::run($input);
print_r($items);

?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM