Last active
September 15, 2019 04:26
-
-
Save stemar/bed52c2555bb1f13838e0cbd03638b18 to your computer and use it in GitHub Desktop.
Convert XML string to JSON string, formatted or not.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function xml_to_json($xml, $formatted=TRUE) { | |
| $options = (bool)$formatted ? JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE : 0; | |
| return json_encode(simplexml_load_string($xml), $options); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $xml = '<Nodes><Node id="1">Content 1</Node><Node id="2">Content 2</Node></Nodes>'; | |
| echo xml_to_json($xml); | |
| /* | |
| { | |
| "Node": [ | |
| "Content 1", | |
| "Content 2" | |
| ] | |
| } | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment