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


/ Published in: PHP
Save to your folder(s)

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/


Copy this code and paste it in your HTML
  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

You need to login to post a comment.