-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamParse.php
More file actions
104 lines (94 loc) · 3.48 KB
/
streamParse.php
File metadata and controls
104 lines (94 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
//$inputFileStr = file_get_contents($_GET['url']);
$inputFileStr = file_get_contents("example.html");
function parseUF($fromString) {
$pattern = "/\<(\w+)\s*[^\>\<]*data-uf=[\"']([^\"']*)[\"'][^\>\<]*>/";//Initial Pattern
$output = array();
$building = 0;
$output[$building] = array();
$path;
$tagToClose;
$thisMatch;
$offset = 0;
$tree = array();
$lastMatchClosed = false;
while(preg_match($pattern,$fromString,$thisMatch,PREG_OFFSET_CAPTURE,$offset)) {
if($thisMatch[1][0] != "") {
$tagToClose = $thisMatch[1][0];
$offset = $thisMatch[0][1] + 4;
$pattern = '/\<(\w+)\s*[^\>\<]*data-uf=["\']([^"\']*)["\'][^\>\<]*>|\<\/('.$tagToClose.')>/';
$path = buildPath($thisMatch[2][0],$tree);
$tree = addToTree($tree,$tagToClose,$path);
$lastMatchClosed = false;
}
else {
if(!$lastMatchClosed) {
$data = getData($fromString,($offset - 4),$tagToClose);
$output[$building] = buildArrayTree($data,end($tree)[1],$output[$building]);
//Tag that is being closed was just opened
//Need to track back for data and add it to the output JSON set
}
$offset = $thisMatch[0][1] + 4;
$tree = removeFromTree($tree,$thisMatch[3][0]);
if(count($tree) == 0) {
$building = $building + 1;
$output[$building] = array();
}
$lastMatchClosed = true;
}
//print_r($thisMatch);
}
return(json_encode($output));
}
function addToTree($tree,$tag,$path) {
array_push($tree,array($tag,$path));
return($tree);
}
function removeFromTree($tree,$tag) {
if(end($tree)[0] == $tag) {
array_pop($tree);
}
else {
trigger_error('Mismatched tag close: '.$tag.' != '.end($tree)[0]);
}
return($tree);
}
function getData($string,$offset,$tag) {
$match;
$pattern = '<'.$tag.'[^\>]*\>([^\<]*)\</'.$tag.'\w*>';
preg_match($pattern,$string,$match,0,$offset);
return($match[1]);
}
function buildPath($path,$tree) {
$pathArr = preg_split('/:/',$path);
if($pathArr[0] == '') {
unset($pathArr[0]);
$pathArr = array_merge(end($tree)[1],$pathArr);
}
return($pathArr);
}
function buildArrayTree($data,$path,$object) {
if($object['uFType'] == NULL) {//Object type not set
$object['uFType'] = $path[0];
}
elseif ($object['uFType'] != $path[0]) {//Object type mismatch
trigger_error('Object type ('.$object['uFType'].') does not match path root:'.$path[0]);
return($object);
}
unset($path[0]);
$ref =& $object;
foreach($path as $pathFragment) {
$tref =& $ref;
unset($ref);
$ref =& $tref[$pathFragment];
unset($tref);
if(($ref == NULL) && ($pathFragment != end($path))) {
$ref = array();
}
}
$ref = $data;
return($object);
}
echo parseUF($inputFileStr);
echo $_GET['url'];
?>