is_serialized

Check value to find if it was serialized.

Description

is_serialized( (string) $data, (bool) $strict = true ); 

If $data is not an string, then returned value will always be false. Serialized data is always a string.

Parameters (2)

0. $data (string)
Value to check to see if was serialized.
1. $strict — Optional. (bool) => true
Whether to be strict about the end of the string. Default true.

Usage

  1. if ( !function_exists( 'is_serialized' ) ) { 
  2. require_once ABSPATH . WPINC . '/functions.php'; 
  3.  
  4. // Value to check to see if was serialized. 
  5. $data = ''; 
  6.  
  7. // Optional. Whether to be strict about the end of the string. Default true. 
  8. $strict = true; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = is_serialized($data, $strict); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/functions.php  
  1. function is_serialized( $data, $strict = true ) { 
  2. // if it isn't a string, it isn't serialized. 
  3. if ( ! is_string( $data ) ) { 
  4. return false; 
  5. $data = trim( $data ); 
  6. if ( 'N;' == $data ) { 
  7. return true; 
  8. if ( strlen( $data ) < 4 ) { 
  9. return false; 
  10. if ( ':' !== $data[1] ) { 
  11. return false; 
  12. if ( $strict ) { 
  13. $lastc = substr( $data, -1 ); 
  14. if ( ';' !== $lastc && '}' !== $lastc ) { 
  15. return false; 
  16. } else { 
  17. $semicolon = strpos( $data, ';' ); 
  18. $brace = strpos( $data, '}' ); 
  19. // Either ; or } must exist. 
  20. if ( false === $semicolon && false === $brace ) 
  21. return false; 
  22. // But neither must be in the first X characters. 
  23. if ( false !== $semicolon && $semicolon < 3 ) 
  24. return false; 
  25. if ( false !== $brace && $brace < 4 ) 
  26. return false; 
  27. $token = $data[0]; 
  28. switch ( $token ) { 
  29. case 's' : 
  30. if ( $strict ) { 
  31. if ( '"' !== substr( $data, -2, 1 ) ) { 
  32. return false; 
  33. } elseif ( false === strpos( $data, '"' ) ) { 
  34. return false; 
  35. // or else fall through 
  36. case 'a' : 
  37. case 'O' : 
  38. return (bool) preg_match( "/^{$token}:[0-9]+:/s", $data ); 
  39. case 'b' : 
  40. case 'i' : 
  41. case 'd' : 
  42. $end = $strict ? '$' : ''; 
  43. return (bool) preg_match( "/^{$token}:[0-9.E-]+;$end/", $data ); 
  44. return false;