Last active
June 21, 2016 19:43
-
-
Save mtio/0261b5641f8998b675a6a6e56fec7396 to your computer and use it in GitHub Desktop.
Takes a string that was a Javascript object and formats it to a JSON String
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 | |
| private function formatJsObjectToJson($jsObjectString) | |
| { | |
| return implode(',', array_map( function($y) { if (!$y) { return '""'; } return $y; }, explode(',', preg_replace_callback('/({|,)(\w*):+/', function($e) { return $e[1] . '"' . $e[2] . '":'; }, $jsObjectString)))); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Takes a string like
'{ key : "value", data: [0,1,2,,,] }'and turns it into'{ "key" : "value", "data": [0,1,2,"","",""] }'