2 // +-----------------------------------------------------------------------------+
3 // Copyright (C) 2011 ZMG LLC <sam@zhservices.com>
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
38 class xmltoarray_parser_htmlfix
{
45 * Default constructor for xmltoarray_parser_htmlfix.
47 function xmltoarray_parser_htmlfix()
49 $this->values
= array();
50 $this->index
= array();
51 $this->thearray
= array();
52 $this->parser
= xml_parser_create();
56 * xmlparser_setoption sets XML options based on xml_parser_set_option options.
57 * @param $optionName - The name of the option from the xml_parser_set_option list.
58 * @param $value - The value to set for the option.
60 function xmlparser_setoption($optionName, $value)
62 xml_parser_set_option($this->parser
, $optionName, $value);
66 * xmlparser_fix_into_struct fixes the XML and passes the XML into the struct parser.
67 * @param $xml - A string XML value.
69 function xmlparser_fix_into_struct($xml)
71 $trans_table = get_html_translation_table(HTML_ENTITIES
,ENT_QUOTES
);
73 foreach($trans_table as $key=>$value) {
74 if($key != "<" && $key != ">" && $key != "&" && $key != "\"" && $key != "'" && $key != " "){
78 foreach($keys as $key=>$value){
79 $xml = preg_replace("/".$key."/",$value,$xml);
81 $xml = str_replace("&","%and%",$xml);
83 xml_parse_into_struct($this->parser
, $xml, $this->values
, $this->index
);
84 xml_parser_free($this->parser
);
88 * createArray creates and returns the array.
89 * @return The associative XML array.
91 function createArray()
94 $name = isset($this->values
[$i]['tag']) ?
$this->values
[$i]['tag']: '';
95 $this->thearray
[$name] = isset($this->values
[$i]['attributes']) ?
$this->values
[$i]['attributes'] : '';
96 $this->thearray
[$name] = $this->_struct_to_array($this->values
, $i);
97 return $this->thearray
;
101 * _struct_to_array is a recursive function that takes the values and creates the array.
102 * @param $values - The values of the XML
103 * @param &$i - The index value
106 function _struct_to_array($values, &$i)
109 if (isset($values[$i]['value'])) array_push($child, $values[$i]['value']);
111 while ($i++
< count($values)) {
112 if(isset($values[$i])){
113 switch ($values[$i]['type']) {
115 array_push($child, $values[$i]['value']);
119 $name = $values[$i]['tag'];
121 $child[$name]= (isset($values[$i]['value']))?
($values[$i]['value']):'';
122 if(isset($values[$i]['attributes'])) {
123 $child[$name] = $values[$i]['attributes'];
129 $name = $values[$i]['tag'];
130 $size = isset($child[$name]) ?
sizeof($child[$name]) : 0;
131 $child[$name][$size] = $this->_struct_to_array($values, $i);
144 * fix_html_entities replaces all instances of '%and%' with '&', since the xml_parser can't handle '&'.
145 * @param $string - A string value.
146 * @return A fixed string with & instead of %and%.
148 function fix_html_entities($string)
150 $string = str_replace("%and%","&",$string);