commit | author | age
|
8bd4d9
|
1 |
<?php |
JK |
2 |
class jsarray { |
|
3 |
static function parse($array) { |
|
4 |
$data = '<?php '.$array.' ?>'; |
|
5 |
$data = token_get_all($data); |
|
6 |
|
|
7 |
$stack = array( array() ); |
|
8 |
$element = NULL; |
|
9 |
foreach($data as $token) { |
|
10 |
if(is_array($token)) { |
|
11 |
// Ignore < ? php and ? > added above |
|
12 |
if($token[0] == T_OPEN_TAG OR $token[0] == T_CLOSE_TAG) continue; |
|
13 |
// String/int element within an array |
|
14 |
if($token[0] == T_CONSTANT_ENCAPSED_STRING || $token[0] == T_LNUMBER) { |
|
15 |
$element = substr($token[1], 1, -1); |
|
16 |
} |
|
17 |
} |
|
18 |
// Nested array |
|
19 |
elseif($token == '[') { |
|
20 |
array_push($stack, array()); |
|
21 |
} |
|
22 |
// End of nested array |
|
23 |
elseif($token == ']') { |
|
24 |
// Put elements into the lastest array |
|
25 |
if($element !== NULL && $element !== FALSE) { |
|
26 |
end($stack); |
|
27 |
$stack[key($stack)][] = $element; |
|
28 |
$element = NULL; |
|
29 |
} |
|
30 |
|
|
31 |
// Check - maybe there are no elements between ] and next ] |
|
32 |
$element = FALSE; |
|
33 |
|
|
34 |
$temp = array_pop($stack); |
|
35 |
end($stack); |
|
36 |
$stack[key($stack)][] = $temp; |
|
37 |
unset($temp); |
|
38 |
} |
|
39 |
// Elements separator |
|
40 |
elseif($token == ',') { |
|
41 |
// Put elements into the lastest array (]] check) |
|
42 |
if($element !== FALSE) { |
|
43 |
end($stack); |
|
44 |
$stack[key($stack)][] = $element; |
|
45 |
} |
|
46 |
$element = NULL; |
|
47 |
} |
|
48 |
else |
|
49 |
{ |
|
50 |
return array(); |
|
51 |
} |
|
52 |
} |
|
53 |
|
|
54 |
return $stack[0][0]; |
|
55 |
} |
|
56 |
} |
|
57 |
?> |