Run
Run

Reputation: 57286

Regex in a json data?

I have a regex in the json data,

$config = '{"regex": "/font-size:\s*(?:.*);/i"}';

And I want to convert the json data to an array,

$config = json_decode($config,true);

var_dump($config);

It returns NULL due to the regex.

How can I get around to it?

So I can get this,

Array
(
    [regex] => "/font-size:\s*(?:.*);/i"
)

is it possible?

edit:

it returns,

array(1) {
  [0]=>
  string(37) '{"regex": "/font-size:\s*(?:.*);/i"}'
}

should be...

array(1) {
  ["regex"]=>
  string(22) "/font-size:\s*(?:.*);/i"
}

Upvotes: 1

Views: 215

Answers (2)

Baba
Baba

Reputation: 95161

Your JSON has JSON_ERROR_SYNTAX so you are having Erros

Option A You should use stripslashes first but you would lose backslash \

$config = '{"regex": "/font-size:\s*(?:.*);/i"}';
$config = json_decode(stripslashes($config),true);
var_dump($config);

Output

array
  'regex' => string '/font-size:s*(?:.*);/i' (length=22)

Option B Create your own function

$config = '{"regex": "/font-size:\s*(?:.*);/i"}';
var_dump(splitJSONObject($config));

Output

array
  'regex' => string '/font-size:\s*(?:.*);/i' (length=23)

Function Used (Only Splits to array)

function splitJSONObject($json) {
    $json = str_replace(array("\\\\","\\\""), array("\","""), $json);
    $parts = preg_split("@(\"[^\"]*\")|([\[\]\{\},:])|\s@is", $json, - 1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
    foreach ( $parts as $index => $part ) {
        if (strlen($part) == 1) {
            switch ($part) {
                case "[" :
                case "{" :
                    $parts[$index] = "array(";
                    break;
                case "]" :
                case "}" :
                    $parts[$index] = ")";
                    break;
                case ":" :
                    $parts[$index] = "=>";
                    break;
                case "," :
                    break;
                default :
                    return null;
            }
        } else {
            if ((substr($part, 0, 1) != "\"") || (substr($part, - 1, 1) != "\"")) {
                return null;
            }
        }
    }
    $json = str_replace(array("\",""","$"), array("\\\\","\\\"","\\$"), implode("", $parts));
    return eval("return $json;");
}

Upvotes: 2

nalply
nalply

Reputation: 28817

Use this:

$config = '{"regex": "/font-size:\\\\s*(?:.*);/i"}';

Note the quadruple backslash.

Rationale: Even in single quoted string the backslash has the escaping function. With '\'' you have a string ' (SINGLE QUOTE ONLY). Therefore you also need to escape the backslash. '\\' is the string \ (BACKSLASH ONLY).

But why quadruple the backslash?

When assigning the variable $config, then you lose two backslashes. They are "used" up.

Then, JSON itself needs the remaining two backslashes.

Execution log:

var_dump(json_decode('{"regex": "/font-size:\\\\s*(?:.*);/i"}'));

class stdClass#2 (1) {
  public $regex =>
  string(23) "/font-size:\\s*(?:.*);/i"
}

Did you see the double backslash in the JSON output? They have to stay, or the JSON would be invalid.

Upvotes: 2

Related Questions