method checkJson documentation in bbn\Str
Checks whether a JSON string is valid or not. If $return_error is set to true, the error will be returned.
Example
var_dump(\bbn\Str::checkJson("{"foo":"bar"}"));
// (bool) true
var_dump(\bbn\Str::checkJson("foo"));
// (bool) false
var_dump(\bbn\Str::checkJson("foo", true));
// (string) "Syntax error, malformed JSON"
function(string $json, bool $return_error = false)
{
json_decode($json);
$error = json_last_error();
if ($error === JSON_ERROR_NONE) {
return true;
}
if (!$return_error) {
return false;
}
switch ($error) {
case JSON_ERROR_DEPTH:
return X::_('Maximum stack depth exceeded');
case JSON_ERROR_STATE_MISMATCH:
return X::_('State mismatch (invalid or malformed JSON)');
case JSON_ERROR_CTRL_CHAR:
return X::_('Unexpected control character found');
case JSON_ERROR_SYNTAX:
return X::_('Syntax error, malformed JSON');
case JSON_ERROR_UTF8:
return X::_('Malformed UTF-8 characters, possibly incorrectly encoded');
default:
return X::_('Unknown error');
}
}
Checks whether a JSON string is valid or not. If $return_error is set to true, the error will be returned. BBN is a suite of PHP and JS libraries and VueJS components - all open-source! bbn.io, build applications, the quick way
This website uses cookies to ensure you get the best experience on our website.
© 2011-2025
BBN Solutions