Created
April 11, 2023 07:24
-
-
Save mrunkel/bc3084d89ea882ef4549cfb3ec37576c to your computer and use it in GitHub Desktop.
Small PHP function to see if a text blob is json or not
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private function isJson($data): bool | |
{ | |
// check that the first character is { or [ | |
if (in_array(substr($data, 0, 1), ['{', '['])) { | |
// if that worked, try to process as json | |
// no errors, return true. | |
$json = json_decode($data); | |
return json_last_error() === JSON_ERROR_NONE; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment