0

I am trying to decode a JSON string using json_decode() in PHP. Part of my JSON string has HTML tags in it. Eg. (For better view of the code go to http://gist.github.com/605906)

$json = '{"productid" : "prod:a8f2d4ef-108e-5fbf-fa74-595ddc0c7950","memo" : "<div style=\"color: #000000; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; background-image: initial; background-repeat: initial; background-attachment: initial; background-color: #ffffff; background-position: initial initial; margin: 8px;\">WS1CI metered clack valve, 1\" for softener  P/N: V1CIDME-03</div>"}';

But I am getting NULL values, when I do

var_dump(json_decode($json));

or

var_dump(json_decode($json, true));
0

3 Answers 3

1

it works for me. If you get a specific error check your PHP installation, but As of PHP 5.2.0 Json comes by default.

object(stdClass)#99 (2) {
  ["productid"]=>
  string(41) "prod:a8f2d4ef-108e-5fbf-fa74-595ddc0c7950"
  ["memo"]=>
  string(59) "WS1CI metered clack valve, 1" for softener  P/N: V1CIDME-03"
}
array(2) {
  ["productid"]=>
  string(41) "prod:a8f2d4ef-108e-5fbf-fa74-595ddc0c7950"
  ["memo"]=>
  string(59) "WS1CI metered clack valve, 1" for softener  P/N: V1CIDME-03"
}

Works even for the html.

object(stdClass)#1 (2) {
  ["productid"]=>
  string(41) "prod:a8f2d4ef-108e-5fbf-fa74-595ddc0c7950"
  ["memo"]=>
  string(328) "<div style="color: #000000; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; background-image: initial; background-repeat: initial; background-attachment: initial; background-color: #ffffff; background-position: initial initial; margin: 8px;">WS1CI metered clack valve, 1" for softener  P/N: V1CIDME-03</div>"
}
Sign up to request clarification or add additional context in comments.

Comments

0

Have a look at your server's error_log. This might yield to some more information.

Comments

-3
<?php
$arr = array();
$json = '{"productid" : "prod:a8f2d4ef-108e-5fbf-fa74-595ddc0c7950","memo" : "&lt;div style=\"color: #000000; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; background-image: initial; background-repeat: initial; background-attachment: initial; background-color: #ffffff; background-position: initial initial; margin: 8px;\"&gt;WS1CI metered clack valve, 1\" for softener  P/N: V1CIDME-03&lt;/div&gt;"}';


var_dump(json_decode($json, true));

?>

You need to transform all html (like < and > into &lt; and &gt;)

1 Comment

This does not answer the question. What happens is, the result of print_r is interpreted by the browser as HTML, so the result seems to be empty, but the JSON-decoded object still contains the correct string, that simply happens to be a bit of HTML that does not display anything. Your answer would allow to let the browser display the actual HTML code, but trying to use the HTML-escaped string for other purposes (creating a DOM element for instance) would probably not yield the expected result.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.