0

I have some chunks of HTML in my database that I put in a JSON this way, upon first page render:

foreach ($data as $key => $value) {
    $output .= '
    "'.$key.'" : "'.addSlashes($value).'",
    ';
}

usually it works fine, except in a few cases where I get a leading line feed like this below:

    "html" : "
    <div id=\"object_01\" class=\"object_textbox\" namn=\"object_01\" style=\"z-index:1;font-family:Arial;left:750.9776000976563px;top:30.979339599609375px;position:absolute;width:709.6444444656372px;height:327.6444444656372px;\"><div class=\"object_inner\" style=\"border:0px solid rgb(0,0,0);background-color:rgb(255,255,255);color:rgb(0,0,0);opacity:.8;font-size:28px;line-height:42px;padding:20px;\">“Behold my Beloved Son, in whom I am well pleased, in whom I have glorified my name—hear ye him.” And it came to pass, as they understood they cast their eyes up again towards heaven; and behold, they saw a Man descending out of heaven; and he was clothed in a white robe; and he came down and stood in the midst of them...</div></div><div id=\"object_02\" class=\"object_textbox\" namn=\"object_02\" style=\"z-index:2;font-family:Arial;position:absolute;top:309.9988098144531px;left:1269.9826049804688px;width:187.6444444656372px;height:48.64444446563721px;\"><div class=\"object_inner\" style=\"border:0px solid rgb(0,0,0);font-style:italic;font-weight:bold;\">3 Nephi 11:7-8</div></div>",

Which obviously breaks the entiry page since javascript doesn't support linefeeds inside strings.

Any idéa what might cause it, or a way around it?

All the data from the database is picked up from a page at form save, so the user never gets to manually insert that linefeed... I just use jQuery element.html() to pick it up so there shouldn't be a linefeed there...

1
  • You should definately have a look at json_encode instead. Commented Jul 16, 2014 at 11:31

1 Answer 1

2

Never build JSON "manually". Use the built in function:

<?php
$output = json_encode($data);

One of the reasons JSON is so popular (other than it being so compatible with JavaScript) is that it is a widely supported serialization scheme. Almost every language and platform now have libraries for working with it so that you can reliably serialize and deserialize your data in any environment for any other.

Once you're using json_encode, whatever ends up in that data is what was there before serialization. So if the line break is still there, it's not the serialization causing it--you'll need to find it in your other code/data.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh.. there is one natively.. thanks =) don't know how I missed that. Making things harder than they need to be here.. =)
The linefeed was not in my data btw, it was caused by my erroneous way of converting to json.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.