Skip to content

Instantly share code, notes, and snippets.

@mtio
Last active June 21, 2016 19:43
Show Gist options
  • Select an option

  • Save mtio/0261b5641f8998b675a6a6e56fec7396 to your computer and use it in GitHub Desktop.

Select an option

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
<?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))));
}
@mtio
Copy link
Author

mtio commented Jun 21, 2016

Takes a string like '{ key : "value", data: [0,1,2,,,] }' and turns it into '{ "key" : "value", "data": [0,1,2,"","",""] }'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment