Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output line breaks or new lines with json_encode?

I am new to JSON format so please forgive my lack of knowledge. But somehow, I need to add some line breaks to my json_encode array so that it will parse with multiple lines. I've searched and searched but nothing fits my situation.

It's outputted as:

Following formats accepted: www.website.com website.com website.net/something

But I'm looking to output the data like this:

Website needs to be in the following formats:  
www.website.com  
website.com  
website.net/something

I've tried:

echo json_encode( 
    array( 'status' => 'failed',
           'message' => "Website needs to be in the following formats:\n
            www.website.com\n
            website.com\n
            website.net/something"
    ), JSON_PRETTY_PRINT);

But Javascript is parsing it as a literal string so the newlines are ignored and outputted. Can I send the data from PHP to javascript using straight JSON format or do I have to use an array?

I've also tried using <br />.

EDIT :
I am outputting the following way:

$.ajax({
    url: "/forms/content_process.php",
    type:'POST',
    data: $(".content_cat").serialize(),
    processData: false,
    dataType: "json",
    success: function(response) {
        if (response.status == "failed") {
            $(".content_status").css( "background-color", "red" );
            $(".content_status").text(response.message).slideDown("normal");
        } else {
            $(".content_status").css( "background-color", "green" );
            $(".content_status").text("Your category was submitted!").slideDown("normal");
        }
    }
});
like image 309
EternalHour Avatar asked Dec 06 '22 00:12

EternalHour


2 Answers

use \n to insert newline character into the string. don't add a real newline as well.

echo json_encode( 
    array( 'status' => 'failed',
           'message' => "Website needs to be in the following\nformats:\nwww.website.com\nwebsite.com\nwebsite.net/something"
    ), JSON_PRETTY_PRINT);

than on the client side before inserting it into your dom, you will need to replace the newlines with <br /> or use white-space: pre. alternatively you could surround each line with paragraph <p></p> tags. to do so have a look here: jQuery text() and newlines.

like image 70
dreamlab Avatar answered Dec 15 '22 11:12

dreamlab


Looking back at this question, it became painfully obvious to me how simple the solution was. The reason I had such a difficulty, was because I needed to use html format.

Of course it wasn't honoring <br />, I was formatting it as text! This was the simplest way to do it for my situation.

array( 'status' => 'failed',
       'message' => "Website needs to be in the following formats:<br />
        www.website.com<br />
        website.com<br />
        website.net/something"
), JSON_PRETTY_PRINT);

$(".content_status").html(response.message).slideDown("normal");
like image 25
EternalHour Avatar answered Dec 15 '22 12:12

EternalHour