Reputation: 109
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.
Upvotes: 0
Views: 111
Reputation: 7880
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
)
)
)
)
)
Upvotes: 0
Reputation: 7880
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
)
)
Upvotes: 1