Skip to content

Instantly share code, notes, and snippets.

@HazemNoor
Last active August 28, 2021 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HazemNoor/8cf2b6f7deea0eef68b826e9abb39afe to your computer and use it in GitHub Desktop.
Save HazemNoor/8cf2b6f7deea0eef68b826e9abb39afe to your computer and use it in GitHub Desktop.
Function to check if a string is valid json string and return data if valid or false otherwise
<?php
/**
* Check if a string is valid json string and return data if valid or false otherwise
*
* @params Same as json_decode args
*
* @return false|mixed
*/
function json_get()
{
$data = call_user_func_array('json_decode', func_get_args());
return (is_string(func_get_arg(0)) && json_last_error() == JSON_ERROR_NONE) ? $data : false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment