minor bug fix
[openemr.git] / library / xmltoarray_parser_htmlfix.php
blobf750f44d3cad9063632929d3f72c7645fec6c494
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{
39 var $values;
40 var $index;
41 var $thearray;
42 var $parser;
44 /**
45 * Default constructor for xmltoarray_parser_htmlfix.
47 function xmltoarray_parser_htmlfix(){
48 $this->values = array();
49 $this->index = array();
50 $this->thearray = array();
51 $this->parser = xml_parser_create();
54 /**
55 * xmlparser_setoption sets XML options based on xml_parser_set_option options.
56 * @param $optionName - The name of the option from the xml_parser_set_option list.
57 * @param $value - The value to set for the option.
59 function xmlparser_setoption($optionName, $value){
60 xml_parser_set_option($this->parser, $optionName, $value);
63 /**
64 * xmlparser_fix_into_struct fixes the XML and passes the XML into the struct parser.
65 * @param $xml - A string XML value.
67 function xmlparser_fix_into_struct($xml){
68 $trans_table = get_html_translation_table(HTML_ENTITIES,ENT_QUOTES);
69 $keys = array();
70 foreach($trans_table as $key=>$value) {
71 if($key != "<" && $key != ">" && $key != "&" && $key != "\"" && $key != "'" && $key != " "){
72 $keys[$key] = $value;
75 foreach($keys as $key=>$value){
76 $xml = preg_replace("/".$key."/",$value,$xml);
78 $xml = str_replace("&","%and%",$xml);
80 xml_parse_into_struct($this->parser, $xml, $this->values, $this->index);
81 xml_parser_free($this->parser);
84 /**
85 * createArray creates and returns the array.
86 * @return The associative XML array.
88 function createArray(){
89 $i = 0;
90 $name = isset($this->values[$i]['tag']) ? $this->values[$i]['tag']: '';
91 $this->thearray[$name] = isset($this->values[$i]['attributes']) ? $this->values[$i]['attributes'] : '';
92 $this->thearray[$name] = $this->_struct_to_array($this->values, $i);
93 return $this->thearray;
94 }//createArray
96 /**
97 * _struct_to_array is a recursive function that takes the values and creates the array.
98 * @param $values - The values of the XML
99 * @param &$i - The index value
100 * @return The child
102 function _struct_to_array($values, &$i){
103 $child = array();
104 if (isset($values[$i]['value'])) array_push($child, $values[$i]['value']);
106 while ($i++ < count($values)) {
107 if(isset($values[$i])){
108 switch ($values[$i]['type']) {
109 case 'cdata':
110 array_push($child, $values[$i]['value']);
111 break;
113 case 'complete':
114 $name = $values[$i]['tag'];
115 if(!empty($name)){
116 $child[$name]= (isset($values[$i]['value']))?($values[$i]['value']):'';
117 if(isset($values[$i]['attributes'])) {
118 $child[$name] = $values[$i]['attributes'];
121 break;
123 case 'open':
124 $name = $values[$i]['tag'];
125 $size = isset($child[$name]) ? sizeof($child[$name]) : 0;
126 $child[$name][$size] = $this->_struct_to_array($values, $i);
127 break;
129 case 'close':
130 return $child;
131 break;
135 return $child;
136 }//_struct_to_array
139 * fix_html_entities replaces all instances of '%and%' with '&', since the xml_parser can't handle '&'.
140 * @param $string - A string value.
141 * @return A fixed string with & instead of %and%.
143 function fix_html_entities($string){
144 $string = str_replace("%and%","&",$string);
145 return $string;