SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    740
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Question Beautyfying HTML/ Strings

    Hi

    Is there anyway in PHP (or in JS) you can beautify or properly indent the following jSON code?

    I tried wrapping them up using "<Pre>" tags but it did not work. When I say beautify i mean the below code should be converted to something like this http://i.imgur.com/4YT5BIt.png


    Code:
    {"result":{"64":{"id":64,"status":"sent","sent":10,"received":10,"remaining":0,"unique_views":8,"viewed":10,"clicked":0,"clicker":0,"hard_bounce":0,"soft_bounce":0,"unsub":0,"complaints":0,"listid":[140],"campaign_name":"18:22","tags":"","subject":"18:22","from_name":"[DEFAULT_FROM_NAME]","from_email":"[DEFAULT_FROM_EMAIL]","reply_to":"[DEFAULT_REPLY_TO]","stats_by_domain":{"gmail.com":{"sent":10,"received":null,"viewed":10,"clicked":0,"hard_bounce":0,"unsub":0,"complaints":0}},"scheduled_date":"2013-02-28 07:00:00","bat_sent":"yes"}}}

    Thanks for any help.

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,192
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Depending on which browser you use you can get JSON prettify plugins which format the JSON in real time when you dump the source to the browser.

  3. #3
    SitePoint Addict Banana Man's Avatar
    Join Date
    Dec 2005
    Posts
    391
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    For testing purposes i often copy json strings into the http://jsonlint.com/ validator which formats the json as your describing.

  4. #4
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    740
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    No, i am actually looking for a PHP/JS plugin or snippet so that i can add it to my code.


    Thanks

  5. #5
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,192
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by cancer10 View Post
    No, i am actually looking for a PHP/JS plugin or snippet so that i can add it to my code.


    Thanks
    Its not worth it, for testing purposes its more beneficial to use a browser tool which is dynamic for every project that you use JSON in compared to using a server side parser which can lag your script.

  6. #6
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    740
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    i was taking a look into http://jsonlint.com/ and it seems they are using some JS to format the JSON properly. Can I not use something like that? And since its JS to it wont be an overhead for the server.

  7. #7
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cancer10 View Post
    Is there anyway in PHP (or in JS) you can beautify or properly indent the following jSON code?
    For what it's worth, as of PHP 5.4.0 there is an option to the json_encode() function called JSON_PRETTY_PRINT which will indent the JSON for you.

    Here's an example with your JSON.
    Salathe
    Software Developer and PHP Manual Author.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •