1

How can I get json string from this array:

$test['author'] = '<p>Written By: <a href="">Someone</a></p>';

echo json_encode($test); // removed erroneous question mark

[in real, this string came from sql select query.]

Any ideas?

4
  • What part of what you've done doesn't work? Yes, you can pass an array to json_encode and it'll work fine. Commented Feb 1, 2012 at 14:23
  • If you're viewing that directly in a browser, most likely the html's being rendered. View the page source and you'll see the raw json string. Commented Feb 1, 2012 at 14:24
  • What do you mean by json string from array? Commented Feb 1, 2012 at 14:24
  • Marc B you're right, I tried to view json output in a browser and then copy&paste it into a json formatter and got error msg:"JSON is not well formated", now I take raw json string from page source, it works fine :) Commented Feb 2, 2012 at 4:44

1 Answer 1

6

Based on Marc B's comment what will probably fix this for you is:

echo htmlspecialchars(json_encode($test));

That way your <p> tags (and any others) will be changed to &lt;p&gt; and such won't be interpreted by your browser as HTML.

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

Comments

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.