forked from LibreHealthIO/lh-ehr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmltoarray_parser_htmlfix.php
150 lines (137 loc) · 4.84 KB
/
xmltoarray_parser_htmlfix.php
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
// +-----------------------------------------------------------------------------+
// Copyright (C) 2011 ZMG LLC <[email protected]>
//
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//
// A copy of the GNU General Public License is included along with this program:
// libreehr/interface/login/GnuGPL.html
// For more information write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// Author: Eldho Chacko <[email protected]>
// Vinish K <[email protected]>
//
// +------------------------------------------------------------------------------+
/*
* Proper usage for xmltoarray_parser_htmlfix class:
* $xmltoarray = new xmltoarray_parser_htmlfix(); //create instance of class
* $xmltoarray->xmlparser_setoption(XML_OPTION_SKIP_WHITE, 1); //set options same as xml_parser_set_option
* $xmltoarray->xmlparser_setoption(XML_OPTION_CASE_FOLDING, 0);
* $xmltoarray->xmlparser_fix_into_struct($xmlstring); //fixes html values for XML
* $array = $xmltoarray->createArray(); //creates an array with fixed html values
* foreach($array as $key => $value){
* $array[$key] = $xmltoarray->fix_html_entities($value); //returns proper html values
* }
*/
class xmltoarray_parser_htmlfix{
var $values;
var $index;
var $thearray;
var $parser;
/**
* Default constructor for xmltoarray_parser_htmlfix.
*/
function xmltoarray_parser_htmlfix(){
$this->values = array();
$this->index = array();
$this->thearray = array();
$this->parser = xml_parser_create();
}
/**
* xmlparser_setoption sets XML options based on xml_parser_set_option options.
* @param $optionName - The name of the option from the xml_parser_set_option list.
* @param $value - The value to set for the option.
*/
function xmlparser_setoption($optionName, $value){
xml_parser_set_option($this->parser, $optionName, $value);
}
/**
* xmlparser_fix_into_struct fixes the XML and passes the XML into the struct parser.
* @param $xml - A string XML value.
*/
function xmlparser_fix_into_struct($xml){
$trans_table = get_html_translation_table(HTML_ENTITIES,ENT_QUOTES);
$keys = array();
foreach($trans_table as $key=>$value) {
if($key != "<" && $key != ">" && $key != "&" && $key != "\"" && $key != "'" && $key != " "){
$keys[$key] = $value;
}
}
foreach($keys as $key=>$value){
$xml = preg_replace("/".$key."/",$value,$xml);
}
$xml = str_replace("&","%and%",$xml);
xml_parse_into_struct($this->parser, $xml, $this->values, $this->index);
xml_parser_free($this->parser);
}
/**
* createArray creates and returns the array.
* @return The associative XML array.
*/
function createArray(){
$i = 0;
$name = isset($this->values[$i]['tag']) ? $this->values[$i]['tag']: '';
$this->thearray[$name] = isset($this->values[$i]['attributes']) ? $this->values[$i]['attributes'] : '';
$this->thearray[$name] = $this->_struct_to_array($this->values, $i);
return $this->thearray;
}//createArray
/**
* _struct_to_array is a recursive function that takes the values and creates the array.
* @param $values - The values of the XML
* @param &$i - The index value
* @return The child
*/
function _struct_to_array($values, &$i){
$child = array();
if (isset($values[$i]['value'])) array_push($child, $values[$i]['value']);
while ($i++ < count($values)) {
if(isset($values[$i])){
switch ($values[$i]['type']) {
case 'cdata':
array_push($child, $values[$i]['value']);
break;
case 'complete':
$name = $values[$i]['tag'];
if(!empty($name)){
$child[$name]= (isset($values[$i]['value']))?($values[$i]['value']):'';
if(isset($values[$i]['attributes'])) {
$child[$name] = $values[$i]['attributes'];
}
}
break;
case 'open':
$name = $values[$i]['tag'];
$size = isset($child[$name]) ? sizeof($child[$name]) : 0;
$child[$name][$size] = $this->_struct_to_array($values, $i);
break;
case 'close':
return $child;
break;
}
}
}
return $child;
}//_struct_to_array
/**
* fix_html_entities replaces all instances of '%and%' with '&', since the xml_parser can't handle '&'.
* @param $string - A string value.
* @return A fixed string with & instead of %and%.
*/
function fix_html_entities($string){
$string = str_replace("%and%","&",$string);
return $string;
}
}
?>