Ser Ender
Ser Ender

Reputation: 209

PHP: json_decode dumping NULL, BOM not found

I've been trying to find out why this 'json_encode'd string isn't parsing out correctly, and came across previously answered questions that had the UTF BOM sequence that was throwing the error, but didn't help me here.

Here's the code that isn't currently working:

//Decode the notes attached to the sig
$aNotes = json_decode($rule->getNotes(),true);
$bom = pack("CCC",0xef,0xbb,0xbf);
if(0 == strncmp($rule->getNotes(),$bom,3))
{
    print('BOM detected - json encoding in UTF-8<br/>');
}
else
{
    print('BOM NOT detected - json encoding correctly<br/>');
}
print('rule->getNotes:<br/>' . $rule->getNotes() .'<br/>');
var_dump($aNotes);

Which generates this result:

BOM NOT detected - json encoding correctly
rule->getNotes:
[{"lDate":"Unknown","sAuthor":"Unknown","sNote":"This is a general purpose Russian spam rule that matches anything starting with 2, 3 or 4 hex digits followed by a domain name ending with .ru -RSK 2010-05-10"},{"lDate":"1295031463082","sAuthor":"Drew Thorstenson","sNote":"this is Ryan's ru rule"}]
NULL

I've run it through JSON Lint, which said it was valid, and An Online JSON Parser which parsed it correctly too.

Any insight would be greatly appreciated.

Upvotes: 1

Views: 724

Answers (1)

meze
meze

Reputation: 15097

The problem isn't BOM. Probably, you performed htmlspecialchars on json string before storing it in your database, so it has HTML entities of quotes instead of quotes. To make it work you can use:

$aNotes = json_decode(htmlspecialchars_decode($rule->getNotes()),true);

Or simply don't do htmlspecialchars when you store JSON data.

Upvotes: 1

Related Questions