Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

json_validate

Description

The json_validate of JSON for PHP checks if a string contains valid JSON.

Syntax

json_validate(
    string $json,
    int $depth = 512,
    int $flags = 0
): bool

Parameters

json

The string to validate.

This function only works with UTF-8 encoded strings.

NOTE: PHP implements a superset of JSON as specified in the original RFC 7159.

depth

Maximum nesting depth of the structure being decoded. The value must be greater than 0, and less than or equal to 2147483647.

flags

Currently only JSON_INVALID_UTF8_IGNORE is accepted.

Return

Returns true if the given string is syntactically valid JSON, otherwise returns false.

Examples

1 · json · false

<?

$json = '{"a":1]}';

$return = json_validate($json);

var_export($return);

?>
false

2 · json · true

<?

$json = '{"a":1}';

$return = json_validate($json);

var_export($return);

?>
true

3 · depth · false

<?

$value =
[
    1,
    2,
    [
        1,
        2,
        [
            1,
            2
        ]
    ]
];

$json = json_encode($value);
$depth = 3;

$return = json_validate($json, $depth);

var_export($return);

?>
false

4 · depth · true

<?

$value =
[
    1,
    2,
    [
        1,
        2,
        [
            1,
            2
        ]
    ]
];

$json = json_encode($value);
$depth = 4;

$return = json_validate($json, $depth);

var_export($return);

?>
true

5 · flags · JSON_INVALID_UTF8_IGNORE · false

<?

$value = "\xaa";

$json = json_encode($value);
$depth = 512;
$flags = JSON_INVALID_UTF8_IGNORE;

$return = json_validate($json, $depth, $flags);

var_export($return);

?>
false

6 · flags · JSON_INVALID_UTF8_IGNORE · true

<?

$value = "\x00";

$json = json_encode($value);
$depth = 512;
$flags = JSON_INVALID_UTF8_IGNORE;

$return = json_validate($json, $depth, $flags);

var_export($return);

?>
true
HomeMenu