clean up pat_ledger report
[openemr.git] / library / xmltoarray_parser_htmlfix.php
blobcec177f8159abe6b0d2374eae3c59372d93dc9c4
1 <?php
2 // +-----------------------------------------------------------------------------+
3 // Copyright (C) 2011 ZMG LLC <sam@zhservices.com>
4 //
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
18 // A copy of the GNU General Public License is included along with this program:
19 // openemr/interface/login/GnuGPL.html
20 // For more information write to the Free Software
21 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 // Author: Eldho Chacko <eldho@zhservices.com>
24 // Vinish K <vinish@zhservices.com>
26 // +------------------------------------------------------------------------------+
28 * Proper usage for xmltoarray_parser_htmlfix class:
29 * $xmltoarray = new xmltoarray_parser_htmlfix(); //create instance of class
30 * $xmltoarray->xmlparser_setoption(XML_OPTION_SKIP_WHITE, 1); //set options same as xml_parser_set_option
31 * $xmltoarray->xmlparser_setoption(XML_OPTION_CASE_FOLDING, 0);
32 * $xmltoarray->xmlparser_fix_into_struct($xmlstring); //fixes html values for XML
33 * $array = $xmltoarray->createArray(); //creates an array with fixed html values
34 * foreach($array as $key => $value){
35 * $array[$key] = $xmltoarray->fix_html_entities($value); //returns proper html values
36 * }
38 class xmltoarray_parser_htmlfix
40 var $values;
41 var $index;
42 var $thearray;
43 var $parser;
45 /**
46 * Default constructor for xmltoarray_parser_htmlfix.
48 function __construct()
50 $this->values = array();
51 $this->index = array();
52 $this->thearray = array();
53 $this->parser = xml_parser_create();
56 /**
57 * xmlparser_setoption sets XML options based on xml_parser_set_option options.
58 * @param $optionName - The name of the option from the xml_parser_set_option list.
59 * @param $value - The value to set for the option.
61 function xmlparser_setoption($optionName, $value)
63 xml_parser_set_option($this->parser, $optionName, $value);
66 /**
67 * xmlparser_fix_into_struct fixes the XML and passes the XML into the struct parser.
68 * @param $xml - A string XML value.
70 function xmlparser_fix_into_struct($xml)
72 $trans_table = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
73 $keys = array();
74 foreach ($trans_table as $key => $value) {
75 if ($key != "<" && $key != ">" && $key != "&" && $key != "\"" && $key != "'" && $key != " ") {
76 $keys[$key] = $value;
80 foreach ($keys as $key => $value) {
81 $xml = preg_replace("/".$key."/", $value, $xml);
84 $xml = str_replace("&", "%and%", $xml);
86 xml_parse_into_struct($this->parser, $xml, $this->values, $this->index);
87 xml_parser_free($this->parser);
90 /**
91 * createArray creates and returns the array.
92 * @return The associative XML array.
94 function createArray()
96 $i = 0;
97 $name = isset($this->values[$i]['tag']) ? $this->values[$i]['tag']: '';
98 $this->thearray[$name] = isset($this->values[$i]['attributes']) ? $this->values[$i]['attributes'] : '';
99 $this->thearray[$name] = $this->_struct_to_array($this->values, $i);
100 return $this->thearray;
101 }//createArray
104 * _struct_to_array is a recursive function that takes the values and creates the array.
105 * @param $values - The values of the XML
106 * @param &$i - The index value
107 * @return The child
109 function _struct_to_array($values, &$i)
111 $child = array();
112 if (isset($values[$i]['value'])) {
113 array_push($child, $values[$i]['value']);
116 while ($i++ < count($values)) {
117 if (isset($values[$i])) {
118 switch ($values[$i]['type']) {
119 case 'cdata':
120 array_push($child, $values[$i]['value']);
121 break;
123 case 'complete':
124 $name = $values[$i]['tag'];
125 if (!empty($name)) {
126 $child[$name]= (isset($values[$i]['value']))?($values[$i]['value']):'';
127 if (isset($values[$i]['attributes'])) {
128 $child[$name] = $values[$i]['attributes'];
131 break;
133 case 'open':
134 $name = $values[$i]['tag'];
135 $size = isset($child[$name]) ? sizeof($child[$name]) : 0;
136 $child[$name][$size] = $this->_struct_to_array($values, $i);
137 break;
139 case 'close':
140 return $child;
141 break;
146 return $child;
147 }//_struct_to_array
150 * fix_html_entities replaces all instances of '%and%' with '&', since the xml_parser can't handle '&'.
151 * @param $string - A string value.
152 * @return A fixed string with & instead of %and%.
154 function fix_html_entities($string)
156 $string = str_replace("%and%", "&", $string);
157 return $string;