chore: ci mariadb stuff - added 11.1 and removed 10.9 (#6834)
[openemr.git] / library / xmltoarray_parser_htmlfix.php
blobb1736d1911185f1fe337a1622b9bd0dffd06836f
1 <?php
3 // +-----------------------------------------------------------------------------+
4 // Copyright (C) 2011 ZMG LLC <sam@zhservices.com>
5 //
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
19 // A copy of the GNU General Public License is included along with this program:
20 // openemr/interface/login/GnuGPL.html
21 // For more information write to the Free Software
22 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 // Author: Eldho Chacko <eldho@zhservices.com>
25 // Vinish K <vinish@zhservices.com>
27 // +------------------------------------------------------------------------------+
29 * Proper usage for xmltoarray_parser_htmlfix class:
30 * $xmltoarray = new xmltoarray_parser_htmlfix(); //create instance of class
31 * $xmltoarray->xmlparser_setoption(XML_OPTION_SKIP_WHITE, 1); //set options same as xml_parser_set_option
32 * $xmltoarray->xmlparser_setoption(XML_OPTION_CASE_FOLDING, 0);
33 * $xmltoarray->xmlparser_fix_into_struct($xmlstring); //fixes html values for XML
34 * $array = $xmltoarray->createArray(); //creates an array with fixed html values
35 * foreach($array as $key => $value){
36 * $array[$key] = $xmltoarray->fix_html_entities($value); //returns proper html values
37 * }
39 class xmltoarray_parser_htmlfix
41 var $values;
42 var $index;
43 var $thearray;
44 var $parser;
46 /**
47 * Default constructor for xmltoarray_parser_htmlfix.
49 function __construct()
51 $this->values = array();
52 $this->index = array();
53 $this->thearray = array();
54 $this->parser = xml_parser_create();
57 /**
58 * xmlparser_setoption sets XML options based on xml_parser_set_option options.
59 * @param $optionName - The name of the option from the xml_parser_set_option list.
60 * @param $value - The value to set for the option.
62 function xmlparser_setoption($optionName, $value)
64 xml_parser_set_option($this->parser, $optionName, $value);
67 /**
68 * xmlparser_fix_into_struct fixes the XML and passes the XML into the struct parser.
69 * @param $xml - A string XML value.
71 function xmlparser_fix_into_struct($xml)
73 $trans_table = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
74 $keys = array();
75 foreach ($trans_table as $key => $value) {
76 if ($key != "<" && $key != ">" && $key != "&" && $key != "\"" && $key != "'" && $key != " ") {
77 $keys[$key] = $value;
81 foreach ($keys as $key => $value) {
82 $xml = preg_replace("/" . $key . "/", $value, $xml);
85 $xml = str_replace("&", "%and%", $xml);
87 xml_parse_into_struct($this->parser, $xml, $this->values, $this->index);
88 xml_parser_free($this->parser);
91 /**
92 * createArray creates and returns the array.
93 * @return The associative XML array.
95 function createArray()
97 $i = 0;
98 $name = isset($this->values[$i]['tag']) ? $this->values[$i]['tag'] : '';
99 $this->thearray[$name] = isset($this->values[$i]['attributes']) ? $this->values[$i]['attributes'] : '';
100 $this->thearray[$name] = $this->_struct_to_array($this->values, $i);
101 return $this->thearray;
102 }//createArray
105 * _struct_to_array is a recursive function that takes the values and creates the array.
106 * @param $values - The values of the XML
107 * @param &$i - The index value
108 * @return The child
110 function _struct_to_array($values, &$i)
112 $child = array();
113 if (isset($values[$i]['value'])) {
114 array_push($child, $values[$i]['value']);
117 while ($i++ < count($values)) {
118 if (isset($values[$i])) {
119 switch ($values[$i]['type']) {
120 case 'cdata':
121 array_push($child, $values[$i]['value']);
122 break;
124 case 'complete':
125 $name = $values[$i]['tag'];
126 if (!empty($name)) {
127 $child[$name] = (isset($values[$i]['value'])) ? ($values[$i]['value']) : '';
128 if (isset($values[$i]['attributes'])) {
129 $child[$name] = $values[$i]['attributes'];
132 break;
134 case 'open':
135 $name = $values[$i]['tag'];
136 $size = isset($child[$name]) ? sizeof($child[$name]) : 0;
137 $child[$name][$size] = $this->_struct_to_array($values, $i);
138 break;
140 case 'close':
141 return $child;
142 break;
147 return $child;
148 }//_struct_to_array
151 * fix_html_entities replaces all instances of '%and%' with '&', since the xml_parser can't handle '&'.
152 * @param $string - A string value.
153 * @return A fixed string with & instead of %and%.
155 function fix_html_entities($string)
157 $string = str_replace("%and%", "&", $string);
158 return $string;