JBZoo\Utils\Ser::is PHP Method

is() public static method

If $data is not an string, then returned value will always be false. Serialized data is always a string.
public static is ( mixed $data ) : boolean
$data mixed Value to check to see if was serialized
return boolean
    public static function is($data)
    {
        // If it isn't a string, it isn't serialized
        if (!is_string($data)) {
            return false;
        }
        $data = trim($data);
        // Is it the serialized NULL value?
        if ($data === 'N;') {
            return true;
        } elseif ($data === 'b:0;' || $data === 'b:1;') {
            // Is it a serialized boolean?
            return true;
        }
        $length = strlen($data);
        // Check some basic requirements of all serialized strings
        if (self::_checkBasic($data, $length)) {
            return false;
        }
        return @unserialize($data) !== false;
    }

Usage Example

Example #1
0
 public function testIs()
 {
     isFalse(Ser::is(1));
     isFalse(Ser::is(null));
     isFalse(Ser::is('s:4:"test;'));
     isFalse(Ser::is('a:0:{}!'));
     isFalse(Ser::is('a:0'));
     isFalse(Ser::is('This is a string'));
     isFalse(Ser::is('a string'));
     isFalse(Ser::is('z:0;'));
     isTrue(Ser::is('N;'));
     isTrue(Ser::is('b:1;'));
     isTrue(Ser::is('a:0:{}'));
     isTrue(Ser::is('O:8:"stdClass":2:{s:5:"prop1";s:5:"Hello";s:5:"prop2";s:5:"World";}'));
 }