0

我有很多这样的文字:

(item (Index Value) (Name Value)(Name2 Value2)(Name3 Value3) (Speciality (Name-a value-a)  (Name-b value-b))

真实例子:

(item(name 256)(Index 1)(Image "Wea001")(Action 1 1)(class weapon sword)(code 1 1 1 1)(country 2)(level 1)(wear 1)   (limit Knight 1)(range 16)(buy 4)(sell 1)(endurance 4)(specialty(aspeed 700)(Attack 3 10)(hit 15)))

现在我想将它们保存在数组 $Items[$Index] 中 -索引值是行中的 (Index XX) -

并且在每个值中,新数组都包含其中的值,例如(使用实线)

$Items[1]{
$Name => 256,
$Image => 'Wea001',
$Action=> '1 1',
$class => 'weapon sword',
...etc
}

我已经使用爆炸将它们保存在主数组中,但使用默认值 0,1,2,3,..etc 而不是行的索引

$items = explode('<br />', $inititemcontent);
for($i = 0; $i < count($items); $i++){
    echo "$items[$i] <br />";
}

PS:索引值永远不会重复,永远不可能有2行具有相同的索引 PPS:通常不会所有的小标签(名称值)都存在,有时全部存在,有时只有一些。

4

2 回答 2

1

像这样的东西会起作用吗?由于该示例只有一行,因此我没有太多工作要做。

<?php
    $string = '(item(name 256)(Index 1)(Image "Wea001")(Action 1 1)(class weapon sword)(code 1 1 1 1)(country 2)(level 1)(wear 1)   (limit Knight 1)(range 16)(buy 4)(sell 1)(endurance 4)(specialty(aspeed 700)(Attack 3 10)(hit 15)))';

    preg_match_all('!\([^()]+\)!s',$string,$parts);

    $items = array();


    foreach($parts as $index=>$temp_array){
        foreach($parts[$index] as $key=>$component){
            $component = preg_replace('![()]!','',$component);
            preg_match_all('!([^ ]+) ([^)]+)!',$component,$component_parts);

            $temp_key = $component_parts[1][0];
            $temp_val = $component_parts[2][0];
            $items[$index][$temp_key]=$temp_val;
        }
    }

    print_r($items);

?>

输出如下所示:

Array
(
[0] => Array
    (
        [name] => 256
        [Index] => 1
        [Image] => "Wea001"
        [Action] => 1 1
        [class] => weapon sword
        [code] => 1 1 1 1
        [country] => 2
        [level] => 1
        [wear] => 1
        [limit] => Knight 1
        [range] => 16
        [buy] => 4
        [sell] => 1
        [endurance] => 4
        [aspeed] => 700
        [Attack] => 3 10
        [hit] => 15
    )

)
于 2013-03-27T22:46:44.190 回答
0

在我写了第一个答案之后,我注意到原始文件的格式类似于 JSON,所以我写了这个:

    <?php
    $string = '(item(name 256)(Index 1)(Image "Wea001")(Action 1 1)(class weapon sword)(code 1 1 1 1)(country 2)(level 1)(wear 1)   (limit Knight 1)(range 16)(buy 4)(sell 1)(endurance 4)(specialty(aspeed 700)(Attack 3 10)(hit 15)))';



    $patterns = array(
    '!\(!',
    '!\)!',
    '!\{([^\{}]+)\{!',
    '!\},\},\}!',
    '!\},([^}])!',
    '!\{([^ ]+) ([^}]+)\}!',
    '!"!',
    "!'!",
    "!\}(,)?!",
    "!\},$!",
    );
    $replacements = array(
    "{",
    "},",
    "{\"$1':[\n{'",
    "}]}]}",
    "},\n$1",
    "{'$1':'$2'}",
    '',
    '"',
    "}$1\n",
    "}"
    );

    $string = preg_replace($patterns,$replacements,$string);
    //print $string;
    $items = json_decode($string,true);

    print_r($items);

    ?>

输出源如下所示:

Array
    (
        [item] => Array
            (
                [0] => Array
                    (
                        [name] => 256
                    )

                [1] => Array
                    (
                        [Index] => 1
                    )

                [2] => Array
                    (
                        [Image] => Wea001
                    )

                [3] => Array
                    (
                        [Action] => 1 1
                    )

                [4] => Array
                    (
                        [class] => weapon sword
                    )

                [5] => Array
                    (
                        [code] => 1 1 1 1
                    )

                [6] => Array
                    (
                        [country] => 2
                    )

                [7] => Array
                    (
                        [level] => 1
                    )

                [8] => Array
                    (
                        [wear] => 1
                    )

                [9] => Array
                    (
                        [limit] => Knight 1
                    )

                [10] => Array
                    (
                        [range] => 16
                    )

                [11] => Array
                    (
                        [buy] => 4
                    )

                [12] => Array
                    (
                        [sell] => 1
                    )

                [13] => Array
                    (
                        [endurance] => 4
                    )

                [14] => Array
                    (
                        [specialty] => Array
                            (
                                [0] => Array
                                    (
                                        [aspeed] => 700
                                    )

                                [1] => Array
                                    (
                                        [Attack] => 3 10
                                    )

                                [2] => Array
                                    (
                                        [hit] => 15
                                    )

                            )

                    )

            )

    )
于 2013-03-28T02:40:36.117 回答