This is my first post, I hope I'm doing everything OK asking my question: I want to create a function which detects whether a string is valid JSON or not. I already read good topic here (Fastest way to check if a string is JSON in PHP?) but I wanted to add the check functionality for the first character as this function will get lot of non-JSON input. Yet it keeps failing, I don't know why...
Here's the function I wrote so far:
function isJson($string){
$string = trim($string);
vardump($string); //for debugging
if($string[0]!='{'||$string[0]!='['){ //check for first char
echo("{$string[0]}!={ OR {$string[0]}!=["); //this actually prints {!={ OR {!=[ every time it comes across valid JSON...
return false;
}
if(json_decode($string)==true)return true;
else return false;
}
Could you help me with this please, as it is starting to drive me insane? Thank you in advance!