Simple method will be:
1 2 3 4 5 6 | if ( is_array (json_decode( $string ,true)) ){ echo true; } else { echo false; } |
This method doesn’t rely on heuristics, uses native php functionality, and is about as future-proof as you’re gonna get; it just tells you straight up whether there were any errors in decoding the string.
1 2 3 4 | function isJson( $string ) { json_decode( $string ); return (json_last_error() == JSON_ERROR_NONE); } |
What do you think?