我的文字有很多这样的行:
(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] - 索引值是行中的(索引XX) -
并且在每个值中,新数组包含其中的值,例如(使用实线)
$Items[1]{
$Name => 256,
$Image => 'Wea001',
$Action=> '1 1',
$class => 'weapon sword',
...etc
}
我已经使用爆炸将它们保存在主数组中,但是使用默认值0,1,2,3,...等不是行的索引
$items = explode('<br />', $inititemcontent);
for($i = 0; $i < count($items); $i++){
echo "$items[$i] <br />";
}
PS:索引值从不重复,永远不会有2行具有相同的索引 PPS:通常不存在所有小标签(名称值),有时候全部存在,有时只有一些。
答案 0 :(得分: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
)
)
答案 1 :(得分: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
)
)
)
)
)