3v4l.org

run code in 300+ PHP versions simultaneously
<?php $_POST['content'] = '<path d="m34.511 143.4v3.0302l54.101-0.63086v-2.078z" points="34.510773,146.42924 88.611514,145.79838 88.611514,143.72037 34.510773,143.39908" fill="#353564"/> <path d="m34.511 146.43 78.119 2.1017 56.193-2.4552-80.211-0.27738z" points="112.6299,148.53093 168.82266,146.07576 88.611514,145.79838 34.510773,146.42924" fill="#afafde"/>'; //first make the post content valid XML $xml = '<xmp>' . $_POST['content'] . '</xmp>'; //load it as a DOMDOcument to parse the desired tags of path $doc = new DOMDocument(); $doc->loadXML($xml); $array = []; //find all of the <path/> tags foreach($doc->getElementsByTagName('path') as $node) { $attributes = []; //convert the attributes into an associative array by attribute name foreach($node->attributes as $attribute) { $attributes[$attribute->name] = $attribute->value; } //store the tag and attributes into array $array[] = (object) [ $node->tagName => $attributes ]; } //convert the PHP array to a JSON string of an array of objects $json = json_encode($array, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT); print_r($json);
Output for 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
[ { "path": { "d": "m34.511 143.4v3.0302l54.101-0.63086v-2.078z", "points": "34.510773,146.42924 88.611514,145.79838 88.611514,143.72037 34.510773,143.39908", "fill": "#353564" } }, { "path": { "d": "m34.511 146.43 78.119 2.1017 56.193-2.4552-80.211-0.27738z", "points": "112.6299,148.53093 168.82266,146.07576 88.611514,145.79838 34.510773,146.42924", "fill": "#afafde" } } ]

preferences:
150.02 ms | 409 KiB | 5 Q