0

i have text with many lines like this:

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

Real example:

(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)))

Now i want to save them in array $Items[$Index] -index value is the (Index XX) in the line-

and in each value new array contain the values in this, for example (using the real line)

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

i already saved them in the master array using the explode, but using default values 0,1,2,3,..etc not the Index of the line

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

PS: the index value never repeated, there can never be 2 lines with the same index PPS: not usually all the small tags (Name Value) exist, some times all of them, some times only some.

2
  • 1
    Why is the data in this format in the first place? Also, your data doesn't match up with the example. Commented Mar 27, 2013 at 22:15
  • Its data file used in game as C++, i wanna use it with PHP so i can only deal with it, its the same example the master tag is (item ), and into it there are small tags (NameOfTheTag ItsValue), also there are one of the tags called Specialty include smaller tags :/ Commented Mar 27, 2013 at 22:22

2 Answers 2

1

Would something like this work? Since there is only one line for the example I didn't have much to go on.

<?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);

?>

Output looks like this:

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
    )

)
Sign up to request clarification or add additional context in comments.

7 Comments

As a note, the last three are supposed to be nested.
Well, that makes it harder. Back to the drawing board.
Are they [items] separated by new lines in the code or semicolons?
new line, i already saved each item in array using: $item = explode('<br />', $inititemcontent); now want to make it set in each one the new array in the : for($i = 0; $i < count($item); $i++){ } and the array arranged by the Index, for example $items[1][name] = 256
OKAY , fixed :D, now can i change the normal auto_increase value in the array with the index value?
|
0

I was noticing that the format of the original file was similar to JSON after I wrote the first answer, so I wrote this:

    <?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);

    ?>

Output Source looks like this:

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
                                    )

                            )

                    )

            )

    )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.