3v4l.org

run code in 300+ PHP versions simultaneously
<?php class XMLParser { private static $xml = null; private static $encoding = 'UTF-8'; /** * Initialize the root XML node [optional] * @param $version * @param $encoding * @param $format_output */ public static function init($version = '1.0', $encoding = 'UTF-8', $format_output = true) { self::$xml = new DomDocument($version, $encoding); self::$xml->formatOutput = $format_output; self::$encoding = $encoding; } /** * Convert an Array to XML * @param string $node_name - name of the root node to be converted * @param array $arr - aray to be converterd * @return DomDocument */ public static function &createXML($node_name, $arr=array()) { $xml = self::getXMLRoot(); $xml->appendChild(self::convert($node_name, $arr)); self::$xml = null; // clear the xml node in the class for 2nd time use. return $xml; } /** * Convert an Array to XML * @param string $node_name - name of the root node to be converted * @param array $arr - aray to be converterd * @return DOMNode */ private static function &convert($node_name, $arr=array()) { //print_arr($node_name); $xml = self::getXMLRoot(); $node = $xml->createElement($node_name); if(is_array($arr)){ // get the attributes first.; if(isset($arr['@attributes'])) { foreach($arr['@attributes'] as $key => $value) { if(!self::isValidTagName($key)) { throw new Exception('[XMLParser] Illegal character in attribute name. attribute: '.$key.' in node: '.$node_name); } $node->setAttribute($key, htmlspecialchars(self::bool2str($value), ENT_QUOTES, self::$encoding)); } unset($arr['@attributes']); //remove the key from the array once done. } // check if it has a value stored in @value, if yes store the value and return // else check if its directly stored as string if(isset($arr['@value'])) { $node->appendChild($xml->createTextNode(htmlspecialchars(self::bool2str($arr['@value']), ENT_QUOTES, self::$encoding))); unset($arr['@value']); //remove the key from the array once done. //return from recursion, as a note with value cannot have child nodes. return $node; } else if(isset($arr['@cdata'])) { $node->appendChild($xml->createCDATASection(self::bool2str($arr['@cdata']))); unset($arr['@cdata']); //remove the key from the array once done. //return from recursion, as a note with cdata cannot have child nodes. return $node; } } //create subnodes using recursion if(is_array($arr)){ // recurse to get the node for that key foreach($arr as $key=>$value){ if(!self::isValidTagName($key)) { throw new Exception('[XMLParser] Illegal character in tag name. tag: '.$key.' in node: '.$node_name); } if(is_array($value) && is_numeric(key($value))) { // MORE THAN ONE NODE OF ITS KIND; // if the new array is numeric index, means it is array of nodes of the same kind // it should follow the parent key name foreach($value as $k=>$v){ $node->appendChild(self::convert($key, $v)); } } else { // ONLY ONE NODE OF ITS KIND $node->appendChild(self::convert($key, $value)); } unset($arr[$key]); //remove the key from the array once done. } } // after we are done with all the keys in the array (if it is one) // we check if it has any text value, if yes, append it. if(!is_array($arr)) { $node->appendChild($xml->createTextNode(htmlspecialchars(self::bool2str($arr), ENT_QUOTES, self::$encoding))); } return $node; } /* * Get the root XML node, if there isn't one, create it. */ private static function getXMLRoot(){ if(empty(self::$xml)) { self::init(); } return self::$xml; } /* * Get string representation of boolean value */ private static function bool2str($v){ //convert boolean to text value. $v = $v === true ? 'true' : $v; $v = $v === false ? 'false' : $v; return $v; } /* * Check if the tag name or attribute name contains illegal characters * Ref: http://www.w3.org/TR/xml/#sec-common-syn */ private static function isValidTagName($tag){ $pattern = '/^[a-z_]+[a-z0-9\:\-\.\_]*[^:]*$/i'; return preg_match($pattern, $tag, $matches) && $matches[0] == $tag; } } /** * XML2Array: A class to convert XML to array in PHP * It returns the array which can be converted back to XML using the Array2XML script * It takes an XML string or a DOMDocument object as an input. * * See Array2XML: http://www.lalit.org/lab/convert-php-array-to-xml-with-attributes * * Author : Lalit Patel * Website: http://www.lalit.org/lab/convert-xml-to-array-in-php-xml2array * License: Apache License 2.0 * http://www.apache.org/licenses/LICENSE-2.0 * Version: 0.1 (07 Dec 2011) * Version: 0.2 (04 Mar 2012) * Fixed typo 'DomDocument' to 'DOMDocument' * * Usage: * $array = XML2Array::createArray($xml); */ class XML2Array { private static $xml = null; private static $encoding = 'UTF-8'; /** * Initialize the root XML node [optional] * @param $version * @param $encoding * @param $format_output */ public static function init($version = '1.0', $encoding = 'UTF-8', $format_output = true) { self::$xml = new DOMDocument($version, $encoding); self::$xml->formatOutput = $format_output; self::$encoding = $encoding; } /** * Convert an XML to Array * @param string $node_name - name of the root node to be converted * @param array $arr - aray to be converterd * @return DOMDocument */ public static function &createArray($input_xml) { $xml = self::getXMLRoot(); if(is_string($input_xml)) { $parsed = $xml->loadXML($input_xml); if(!$parsed) { throw new Exception('[XML2Array] Error parsing the XML string.'); } } else { if(get_class($input_xml) != 'DOMDocument') { throw new Exception('[XML2Array] The input XML object should be of type: DOMDocument.'); } $xml = self::$xml = $input_xml; } $array[$xml->documentElement->tagName] = self::convert($xml->documentElement); self::$xml = null; // clear the xml node in the class for 2nd time use. return $array; } /** * Convert an Array to XML * @param mixed $node - XML as a string or as an object of DOMDocument * @return mixed */ private static function &convert($node) { $output = array(); switch ($node->nodeType) { case XML_CDATA_SECTION_NODE: $output['@cdata'] = trim($node->textContent); break; case XML_TEXT_NODE: $output = trim($node->textContent); break; case XML_ELEMENT_NODE: // for each child node, call the covert function recursively for ($i=0, $m=$node->childNodes->length; $i<$m; $i++) { $child = $node->childNodes->item($i); $v = self::convert($child); if(isset($child->tagName)) { $t = $child->tagName; // assume more nodes of same kind are coming if(!isset($output[$t])) { $output[$t] = array(); } $output[$t][] = $v; } else { //check if it is not an empty text node if($v !== '') { $output = $v; } } } if(is_array($output)) { // if only one node of its kind, assign it directly instead if array($value); foreach ($output as $t => $v) { if(is_array($v) && count($v)==1) { $output[$t] = $v[0]; } } if(empty($output)) { //for empty nodes $output = ''; } } // loop through the attributes and collect them if($node->attributes->length) { $a = array(); foreach($node->attributes as $attrName => $attrNode) { $a[$attrName] = (string) $attrNode->value; } // if its an leaf node, store the value in @value instead of directly storing it. if(!is_array($output)) { $output = array('@value' => $output); } $output['@attributes'] = $a; } break; } return $output; } /* * Get the root XML node, if there isn't one, create it. */ private static function getXMLRoot(){ if(empty(self::$xml)) { self::init(); } return self::$xml; } } echo XMLParser;

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.40.0060.01018.67
8.3.30.0170.00019.01
8.3.20.0030.00520.34
8.3.10.0080.00023.71
8.3.00.0080.00019.13
8.2.170.0060.00922.96
8.2.160.0070.00720.57
8.2.150.0000.00824.18
8.2.140.0000.00724.66
8.2.130.0070.01026.16
8.2.120.0040.00422.09
8.2.110.0050.00521.08
8.2.100.0110.00017.91
8.2.90.0080.00019.40
8.2.80.0040.00418.04
8.2.70.0030.00617.75
8.2.60.0080.00017.93
8.2.50.0060.00318.07
8.2.40.0000.00818.34
8.2.30.0030.00617.98
8.2.20.0030.00517.82
8.2.10.0050.00318.15
8.2.00.0000.01017.84
8.1.270.0040.00422.31
8.1.260.0040.00426.35
8.1.250.0000.00828.09
8.1.240.0090.00022.36
8.1.230.0040.00820.83
8.1.220.0000.00818.54
8.1.210.0050.00320.81
8.1.200.0030.00617.23
8.1.190.0040.00417.40
8.1.180.0000.00818.10
8.1.170.0030.00518.71
8.1.160.0080.00022.18
8.1.150.0050.00318.74
8.1.140.0060.00317.47
8.1.130.0000.00817.74
8.1.120.0040.00417.41
8.1.110.0030.00817.39
8.1.100.0000.00717.51
8.1.90.0050.00317.47
8.1.80.0040.00417.50
8.1.70.0000.00717.48
8.1.60.0030.00517.69
8.1.50.0030.00617.46
8.1.40.0040.00417.58
8.1.30.0000.00817.62
8.1.20.0000.00917.60
8.1.10.0040.00417.46
8.1.00.0040.00417.33
8.0.300.0000.00719.98
8.0.290.0070.00017.18
8.0.280.0000.00718.48
8.0.270.0000.00717.31
8.0.260.0030.00317.24
8.0.250.0030.00317.07
8.0.240.0030.00317.00
8.0.230.0030.00417.10
8.0.220.0000.00817.00
8.0.210.0070.00016.99
8.0.200.0030.00317.15
8.0.190.0040.00417.08
8.0.180.0000.00716.97
8.0.170.0040.00417.04
8.0.160.0000.00716.88
8.0.150.0050.00216.95
8.0.140.0000.00816.99
8.0.130.0030.00313.36
8.0.120.0040.00416.93
8.0.110.0070.00316.95
8.0.100.0000.00817.02
8.0.90.0030.00517.04
8.0.80.0120.00616.99
8.0.70.0070.00017.00
8.0.60.0030.00616.94
8.0.50.0040.00416.88
8.0.30.0130.01117.12
8.0.20.0110.01117.40
8.0.10.0020.00517.23
8.0.00.0130.00816.80
7.4.330.0030.00315.00
7.4.320.0000.00816.46
7.4.300.0030.00316.55
7.4.290.0070.00016.59
7.4.280.0000.00716.53
7.4.270.0040.00316.42
7.4.260.0080.00016.64
7.4.250.0070.00016.60
7.4.240.0060.00216.57
7.4.230.0000.00816.74
7.4.220.0090.00916.56
7.4.210.0060.01016.61
7.4.200.0000.00816.65
7.4.190.0040.00416.49
7.4.160.0060.01216.59
7.4.150.0110.00717.40
7.4.140.0110.01017.86
7.4.130.0060.01116.59
7.4.120.0130.00416.69
7.4.110.0090.00916.62
7.4.100.0090.00916.44
7.4.90.0080.00816.41
7.4.80.0110.00919.39
7.4.70.0040.01316.62
7.4.60.0130.00316.66
7.4.50.0000.00816.53
7.4.40.0060.00922.77
7.4.30.0100.00716.68
7.4.10.0090.00914.93
7.4.00.0040.01215.03
7.3.330.0000.00513.20
7.3.320.0000.00613.14
7.3.310.0000.00716.14
7.3.300.0070.00016.34
7.3.290.0130.00316.38
7.3.280.0070.01216.33
7.3.270.0060.01217.40
7.3.260.0140.00316.49
7.3.250.0120.00916.49
7.3.240.0140.00516.40
7.3.230.0070.01016.41
7.3.210.0070.01016.51
7.3.200.0070.01719.39
7.3.190.0120.00916.64
7.3.180.0130.00316.41
7.3.170.0100.00716.34
7.3.160.0090.00816.41
7.3.130.0070.01014.95
7.3.120.0070.01114.88
7.3.110.0070.01414.89
7.3.100.0060.01014.71
7.3.90.0050.00914.85
7.3.80.0020.00914.74
7.3.70.0050.01014.69
7.3.60.0030.00814.84
7.3.50.0060.00414.91
7.3.40.0080.00814.69
7.3.30.0030.00814.90
7.3.20.0060.00816.63
7.3.10.0060.00616.39
7.3.00.0100.00716.40
7.2.330.0070.01016.56
7.2.320.0130.00616.41
7.2.310.0060.01216.72
7.2.300.0100.00716.70
7.2.290.0000.01716.71
7.2.260.0080.00815.21
7.2.250.0010.01515.01
7.2.240.0060.01115.05
7.2.230.0080.01014.88
7.2.220.0100.00514.92
7.2.210.0090.00514.95
7.2.200.0030.01114.88
7.2.190.0040.01114.79
7.2.180.0030.00915.02
7.2.170.0020.01014.91
7.2.160.0140.00314.84
7.2.150.0030.01216.65
7.2.140.0130.00016.79
7.2.130.0110.00416.68
7.2.120.0090.01116.74
7.2.110.0090.01216.79
7.2.100.0090.00916.37
7.2.90.0070.01016.74
7.2.80.0130.00216.59
7.2.70.0080.00816.62
7.2.60.0060.00816.74
7.2.50.0090.00716.34
7.2.40.0090.00916.77
7.2.30.0080.00816.76
7.2.20.0090.00816.74
7.2.10.0070.00716.58
7.2.00.0080.00817.59
7.1.330.0090.00415.58
7.1.320.0080.00815.65
7.1.310.0070.00615.46
7.1.300.0050.00915.64
7.1.290.0040.00915.62
7.1.280.0030.00915.63
7.1.270.0050.00815.70
7.1.260.0070.00515.50
7.1.250.0080.00815.44
7.1.240.0030.00715.64
7.1.230.0040.00815.63
7.1.220.0050.00515.44
7.1.210.0030.00915.60
7.1.200.0030.00915.61
7.1.190.0040.00815.69
7.1.180.0060.00615.59
7.1.170.0040.01115.63
7.1.160.0000.01215.75
7.1.150.0110.00315.78
7.1.140.0030.00615.54
7.1.130.0100.00315.65
7.1.120.0000.01415.32
7.1.110.0030.00915.80
7.1.100.0030.01016.86
7.1.90.0030.01015.88
7.1.80.0030.00615.48
7.1.70.0020.00816.30
7.1.60.0080.01017.61
7.1.50.0030.01416.25
7.1.40.0090.00315.62
7.1.30.0060.00615.73
7.1.20.0100.00315.51
7.1.10.0000.01115.37
7.1.00.0050.04319.01
7.0.330.0110.00015.43
7.0.320.0080.00515.26
7.0.310.0040.01415.21
7.0.300.0090.00015.22
7.0.290.0030.00815.24
7.0.280.0040.00415.09
7.0.270.0000.01315.11
7.0.260.0040.00415.35
7.0.250.0070.00715.43
7.0.240.0000.00915.02
7.0.230.0090.00315.31
7.0.220.0080.00314.95
7.0.210.0000.00815.18
7.0.200.0040.00515.07
7.0.190.0070.00415.38
7.0.180.0030.00715.26
7.0.170.0030.00615.29
7.0.160.0130.00015.37
7.0.150.0070.00715.34
7.0.140.0030.04318.72
7.0.130.0070.00715.12
7.0.120.0070.00715.22
7.0.110.0040.00415.12
7.0.100.0090.03417.70
7.0.90.0080.03117.68
7.0.80.0150.04017.62
7.0.70.0120.04017.63
7.0.60.0090.03517.68
7.0.50.0130.02517.86
7.0.40.0040.03816.69
7.0.30.0030.04216.62
7.0.20.0030.05016.69
7.0.10.0080.02516.69
7.0.00.0040.04716.62
5.6.400.0090.00614.23
5.6.390.0000.01214.04
5.6.380.0040.00814.03
5.6.370.0060.00314.55
5.6.360.0030.01214.46
5.6.350.0040.00714.18
5.6.340.0030.01014.61
5.6.330.0110.00413.99
5.6.320.0030.00914.43
5.6.310.0000.01013.96
5.6.300.0090.00614.11
5.6.290.0110.00014.14
5.6.280.0090.03817.66
5.6.270.0120.00314.27
5.6.260.0030.00714.61
5.6.250.0080.03917.47
5.6.240.0130.03417.34
5.6.230.0020.05017.49
5.6.220.0070.03717.44
5.6.210.0060.04517.52
5.6.200.0050.03017.70
5.6.190.0080.02817.68
5.6.180.0020.04717.82
5.6.170.0080.04417.51
5.6.160.0130.03817.54
5.6.150.0050.05017.56
5.6.140.0050.02817.51
5.6.130.0050.04717.70
5.6.120.0050.04817.48
5.6.110.0080.04217.65
5.6.100.0060.04117.82
5.6.90.0100.04217.58
5.6.80.0100.04217.28
5.6.70.0070.04017.35
5.6.60.0070.04017.22
5.6.50.0070.04417.25
5.6.40.0090.03917.24
5.6.30.0050.03117.14
5.6.20.0070.04017.28
5.6.10.0130.03817.20
5.6.00.0050.04317.18
5.5.380.0070.02917.31
5.5.370.0100.04317.50
5.5.360.0090.02617.24
5.5.350.0080.04217.36
5.5.340.0100.04017.33
5.5.330.0080.04217.48
5.5.320.0100.04017.39
5.5.310.0050.04617.26
5.5.300.0070.04217.50
5.5.290.0040.02617.33
5.5.280.0140.03917.59
5.5.270.0070.04017.34
5.5.260.0080.04317.67
5.5.250.0020.03217.46
5.5.240.0050.03816.96
5.5.230.0050.03217.04
5.5.220.0070.02417.25
5.5.210.0030.04717.00
5.5.200.0070.02817.13
5.5.190.0060.03916.97
5.5.180.0080.04217.03
5.5.170.0040.00913.63
5.5.160.0060.04617.13
5.5.150.0020.04416.97
5.5.140.0070.02317.29
5.5.130.0040.04317.32
5.5.120.0070.04417.29
5.5.110.0070.04417.23
5.5.100.0100.04017.06
5.5.90.0050.04217.18
5.5.80.0130.03816.93
5.5.70.0050.04516.87
5.5.60.0080.02317.00
5.5.50.0070.04017.06
5.5.40.0100.04016.96
5.5.30.0030.03117.09
5.5.20.0100.03817.03
5.5.10.0080.04216.91
5.5.00.0050.04016.91
5.4.450.0060.02715.14
5.4.440.0030.04115.18
5.4.430.0070.02715.23
5.4.420.0020.02615.24
5.4.410.0110.03315.11
5.4.400.0070.04015.07
5.4.390.0050.04014.99
5.4.380.0070.04114.86
5.4.370.0020.03914.98
5.4.360.0030.04114.88
5.4.350.0050.03815.02
5.4.340.0030.02815.14
5.4.330.0040.00810.82
5.4.320.0050.02314.80
5.4.310.0060.02614.98
5.4.300.0020.02915.05
5.4.290.0080.03814.89
5.4.280.0070.04314.96
5.4.270.0050.03615.03
5.4.260.0120.03814.79
5.4.250.0070.04014.77
5.4.240.0020.03914.94
5.4.230.0050.04014.98
5.4.220.0020.03615.05
5.4.210.0060.01814.94
5.4.200.0030.03714.94
5.4.190.0030.04614.88
5.4.180.0070.04015.05
5.4.170.0050.03814.84
5.4.160.0030.02714.83
5.4.150.0060.02615.07
5.4.140.0080.03813.73
5.4.130.0050.03713.69
5.4.120.0080.02713.53
5.4.110.0080.04013.59
5.4.100.0120.02713.63
5.4.90.0100.02013.65
5.4.80.0060.03913.60
5.4.70.0050.03213.82
5.4.60.0050.02213.72
5.4.50.0070.03513.64
5.4.40.0060.01913.68
5.4.30.0060.02113.64
5.4.20.0000.02413.63
5.4.10.0050.02213.68
5.4.00.0030.02013.33
5.3.290.0020.03813.17
5.3.280.0030.02313.17
5.3.270.0070.02313.17
5.3.260.0040.02413.17
5.3.250.0030.04113.17
5.3.240.0080.03713.17
5.3.230.0030.02613.17
5.3.220.0030.04013.17
5.3.210.0070.04013.17
5.3.200.0030.03013.17
5.3.190.0080.04013.17
5.3.180.0130.02213.17
5.3.170.0110.03213.17
5.3.160.0050.03513.17
5.3.150.0030.03913.17
5.3.140.0030.02313.17
5.3.130.0070.02513.17
5.3.120.0020.02513.17
5.3.110.0020.02213.17
5.3.100.0020.02213.17
5.3.90.0020.02113.17
5.3.80.0050.02013.17
5.3.70.0020.02413.17
5.3.60.0020.02213.17
5.3.50.0070.01813.17
5.3.40.0020.02413.17
5.3.30.0080.01713.17
5.3.20.0030.02013.17
5.3.10.0000.02813.17
5.3.00.0020.02213.17

preferences:
42.15 ms | 400 KiB | 5 Q