Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: regex in JSON causes problem for json_decode? [duplicate]

I have some regular expressions in my JSON, which doesn't seem to be a problem when I test my JSON on an online JSON validator. However, when I take that JSON string and try to json_decode() in PHP, I get a JSON_ERROR_SYNTAX.

Any ideas why? And how do I solve this?

Sample code:

<?php

$json = <<<EOD
{
  "regex": [
    "Hello\s+World"
  ]
}
EOD;

json_decode($json);

switch (json_last_error()) {
    case JSON_ERROR_NONE:
        echo ' - No errors';
    break;
    case JSON_ERROR_DEPTH:
        echo ' - Maximum stack depth exceeded';
    break;
    case JSON_ERROR_STATE_MISMATCH:
        echo ' - Underflow or the modes mismatch';
    break;
    case JSON_ERROR_CTRL_CHAR:
        echo ' - Unexpected control character found';
    break;
    case JSON_ERROR_SYNTAX:
        echo ' - Syntax error, malformed JSON';
    break;
    case JSON_ERROR_UTF8:
        echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
    break;
    default:
        echo ' - Unknown error';
    break;
}

The problem is in the \s. Changing it to \\s doesn't help.

like image 319
StackOverflowNewbie Avatar asked Jan 19 '19 02:01

StackOverflowNewbie


1 Answers

When you write "\s" in PHP the literal string is \s because \s is not a valid escape sequence.

When you write "\\s" in PHP the literal string is \s because \\ is a valid escape sequence.

JSON, on the other hand will throw an error for invalid escape sequences, which is your issue.

The solution: Don't write JSON by hand.

$json = json_encode(['regex'=> ['Hello\s+World']]);

Output: {"regex":["Hello\\s+World"]} [note: literal string, valid JSON]

The Bad Solution That's More Trouble than it's Worth and Will Probably Cause Problems Down the Line: "Hello\\\s+World" welcome to escaping hell.

like image 185
Sammitch Avatar answered Sep 22 '22 17:09

Sammitch