Jump to content

:{ not only {


hackalive

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.