hackalive Posted April 11, 2012 Share Posted April 11, 2012 Hi guys I have this code I am using function indent($json) { $result = ''; $pos = 0; $strLen = strlen($json); $indentStr = ' '; $newLine = "\n"; $prevChar = ''; $outOfQuotes = true; for ($i=0; $i<=$strLen; $i++) { // Grab the next character in the string. $char = substr($json, $i, 1); // Are we inside a quoted string? if ($char == '"' && $prevChar != '\\') { $outOfQuotes = !$outOfQuotes; // If this character is the end of an element, // output a new line and indent the next line. } else if(($char == '}' || $char == ']') && $outOfQuotes) { $result .= $newLine; $pos --; for ($j=0; $j<$pos; $j++) { $result .= $indentStr; } } // Add the character to the result string. $result .= $char; // If the last character was the beginning of an element, // output a new line and indent the next line. if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) { $result .= $newLine; if ($char == '{' || $char == '[') { $pos ++; } for ($j = 0; $j < $pos; $j++) { $result .= $indentStr; } } $prevChar = $char; } return $result; } However it outputs: { "timeline":{ "headline":"FifteenTwo", "type":"default", "startDate":"2012,1" } } When I would rather it be: { "timeline": { "headline":"FifteenTwo", "type":"default", "startDate":"2012,1" } } If anyone can help me do this that would be great. Thanks in advance A PS to the Admin's, the PHP button is outputing [m][/m] so I had to manually do the PHP tags. Link to comment https://forums.phpfreaks.com/topic/260730-not-only/ Share on other sites More sharing options...
MMDE Posted April 11, 2012 Share Posted April 11, 2012 Hi guys I have this code I am using function indent($json) { $result = ''; $pos = 0; $strLen = strlen($json); $indentStr = ' '; $newLine = "\n"; $prevChar = ''; $outOfQuotes = true; for ($i=0; $i<=$strLen; $i++) { // Grab the next character in the string. $char = substr($json, $i, 1); // Are we inside a quoted string? if ($char == '"' && $prevChar != '\\') { $outOfQuotes = !$outOfQuotes; // If this character is the end of an element, // output a new line and indent the next line. } else if(($char == '}' || $char == ']') && $outOfQuotes) { $result .= $newLine; $pos --; for ($j=0; $j<$pos; $j++) { $result .= $indentStr; } } // Add the character to the result string. $result .= $char; // If the last character was the beginning of an element, // output a new line and indent the next line. if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) { $result .= $newLine; if ($char == '{' || $char == '[') { $pos ++; } for ($j = 0; $j < $pos; $j++) { $result .= $indentStr; } } $prevChar = $char; } return $result; } However it outputs: { "timeline":{ "headline":"FifteenTwo", "type":"default", "startDate":"2012,1" } } When I would rather it be: { "timeline": { "headline":"FifteenTwo", "type":"default", "startDate":"2012,1" } } If anyone can help me do this that would be great. Thanks in advance A PS to the Admin's, the PHP button is outputing [m][/m] so I had to manually do the PHP tags. Actually, the only difference between those two outputs are not even in the function.... EDIT: Or actually, there is another difference I just noticed, one of them has more spaces, and that is set in the function, but I doubt that is what you got problems with... Link to comment https://forums.phpfreaks.com/topic/260730-not-only/#findComment-1336323 Share on other sites More sharing options...
hackalive Posted April 11, 2012 Author Share Posted April 11, 2012 My point is I dont want this :{ I want : { like that adda break between : and { Link to comment https://forums.phpfreaks.com/topic/260730-not-only/#findComment-1336324 Share on other sites More sharing options...
MMDE Posted April 11, 2012 Share Posted April 11, 2012 My point is I dont want this :{ I want : { like that adda break between : and { That was what I figured, but is any of it in the script you posted??? I mean, of course the data is there, but the : and the { and timeline etc it's nowhere to be found being added to the string in the function. Is this done outside the function, is this added onto the string the function returns? Or do you simply want us to copy paste what you wrote and add the needed stuff before and after the string the function return before the string is returned? =P Link to comment https://forums.phpfreaks.com/topic/260730-not-only/#findComment-1336334 Share on other sites More sharing options...
hackalive Posted April 11, 2012 Author Share Posted April 11, 2012 Just tell me what I need to add to make it do as I want when the string is passed into it. Link to comment https://forums.phpfreaks.com/topic/260730-not-only/#findComment-1336336 Share on other sites More sharing options...
MMDE Posted April 11, 2012 Share Posted April 11, 2012 Just tell me what I need to add to make it do as I want when the string is passed into it. Give me a sample text string that you are giving the function, and tell me exactly what the function should return. Link to comment https://forums.phpfreaks.com/topic/260730-not-only/#findComment-1336337 Share on other sites More sharing options...
hackalive Posted April 11, 2012 Author Share Posted April 11, 2012 However it outputs: Code: [select] { "timeline":{ "headline":"FifteenTwo", "type":"default", "startDate":"2012,1" } } When I would rather it be: Code: [select] { "timeline": { "headline":"FifteenTwo", "type":"default", "startDate":"2012,1" } } Link to comment https://forums.phpfreaks.com/topic/260730-not-only/#findComment-1336338 Share on other sites More sharing options...
hackalive Posted April 11, 2012 Author Share Posted April 11, 2012 I tried if (($char == ':{' | $char == ',' || $char == '{' || $char == '[') && $outOfQuotes) { But that didnt help Link to comment https://forums.phpfreaks.com/topic/260730-not-only/#findComment-1336339 Share on other sites More sharing options...
trq Posted April 11, 2012 Share Posted April 11, 2012 A PS to the Admin's, the PHP button is outputing () so I had to manually do the PHP tags. The php button links to the manual. eg; [m]fwrite[/m], will output fwrite. Link to comment https://forums.phpfreaks.com/topic/260730-not-only/#findComment-1336340 Share on other sites More sharing options...
MMDE Posted April 11, 2012 Share Posted April 11, 2012 However it outputs: Code: [select] { "timeline":{ "headline":"FifteenTwo", "type":"default", "startDate":"2012,1" } } When I would rather it be: Code: [select] { "timeline": { "headline":"FifteenTwo", "type":"default", "startDate":"2012,1" } } I understand this, but in the code you gave us the following is nowhere to be found, even less added to the string the function returns: "timeline": and { So how are we supposed to be able to change how those two are separated (or not separated)? Give us the entire code or give us a sample text string that you are giving the function, and tell us exactly what the function should return after getting that text string. Link to comment https://forums.phpfreaks.com/topic/260730-not-only/#findComment-1336342 Share on other sites More sharing options...
trq Posted April 11, 2012 Share Posted April 11, 2012 What exactly are you going to be using this json for? Is it for display purposes? I just don't see the point in your formatting it otherwise. Link to comment https://forums.phpfreaks.com/topic/260730-not-only/#findComment-1336344 Share on other sites More sharing options...
hackalive Posted April 11, 2012 Author Share Posted April 11, 2012 Yup for formatting Link to comment https://forums.phpfreaks.com/topic/260730-not-only/#findComment-1336346 Share on other sites More sharing options...
hackalive Posted April 11, 2012 Author Share Posted April 11, 2012 If you take a careful look at the function it has the flow of how it gets to the characters etc You can pass ANY JSON string through it. Not just a "timeline" one. http://recursive-design.com/blog/2008/03/11/format-json-with-php/ Link to comment https://forums.phpfreaks.com/topic/260730-not-only/#findComment-1336348 Share on other sites More sharing options...
trq Posted April 11, 2012 Share Posted April 11, 2012 Yup for formatting But what will the data be used for? Link to comment https://forums.phpfreaks.com/topic/260730-not-only/#findComment-1336349 Share on other sites More sharing options...
hackalive Posted April 11, 2012 Author Share Posted April 11, 2012 its JSON its being passed into PHP. I just would like to format it nicely for debug to start with then im happy to let it be all standard and un-formatted. Link to comment https://forums.phpfreaks.com/topic/260730-not-only/#findComment-1336352 Share on other sites More sharing options...
MMDE Posted April 11, 2012 Share Posted April 11, 2012 If you take a careful look at the function it has the flow of how it gets to the characters etc You can pass ANY JSON string through it. Not just a "timeline" one. http://recursive-design.com/blog/2008/03/11/format-json-with-php/ So what I ask for is in the string the function gets? =\ It's probably pretty simple to add, but sorry, I'm not going to put myself through that script. Link to comment https://forums.phpfreaks.com/topic/260730-not-only/#findComment-1336353 Share on other sites More sharing options...
trq Posted April 11, 2012 Share Posted April 11, 2012 its JSON its being passed into PHP. I just would like to format it nicely for debug to start with then im happy to let it be all standard and un-formatted. These easiest way to do that is simply: print_r(json_decode($some_json_string)); Link to comment https://forums.phpfreaks.com/topic/260730-not-only/#findComment-1336358 Share on other sites More sharing options...
hackalive Posted April 11, 2012 Author Share Posted April 11, 2012 im encoding NOT decoding Link to comment https://forums.phpfreaks.com/topic/260730-not-only/#findComment-1336370 Share on other sites More sharing options...
trq Posted April 11, 2012 Share Posted April 11, 2012 That doesn't make sense. Link to comment https://forums.phpfreaks.com/topic/260730-not-only/#findComment-1336382 Share on other sites More sharing options...
kicken Posted April 11, 2012 Share Posted April 11, 2012 Try changing the line: if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) { to if (($char == ':' || $char == ',' || $char == '{' || $char == '[') && $outOfQuotes) { Link to comment https://forums.phpfreaks.com/topic/260730-not-only/#findComment-1336487 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.