Posted By

jatubio on 01/20/11


Tagged

String json pregmatch


Versions (?)

Check if a string it's json encoded using regular expressions


 / Published in: PHP
 

Check if a string it's json encoded using regular expressions

Thanks to Jens Anders Bakke for his mootools code published in webfreak.no

http://webfreak.no/wp/2007/09/07/jsontest-for-mootools/

  1. // Comprueba si una cadena es json o no.
  2. // 19/01/2011 Jatubio - http://webfreak.no/wp/2007/09/07/jsontest-for-mootools/
  3. function is_json($string)
  4. {
  5. return !empty($string) && is_string($string) && preg_match('/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/',$string);
  6. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: jatubio on January 28, 2011

It's not working for me..

isjson(jsonencode(array("Id"=>39))); return true

but

isjson(jsonencode(array("SessionId"=>39))); return false

You need to login to post a comment.