JBZoo\Utils\Ser::maybeUn PHP Method

maybeUn() public static method

Unserialize value only if it is serialized.
public static maybeUn ( string $data ) : mixed
$data string A variable that may or may not be serialized
return mixed
    public static function maybeUn($data)
    {
        // If it isn't a string, it isn't serialized
        if (!is_string($data)) {
            return $data;
        }
        $data = trim($data);
        // Is it the serialized NULL value?
        if ($data === 'N;') {
            return null;
        }
        $length = strlen($data);
        // Check some basic requirements of all serialized strings
        if (self::_checkBasic($data, $length)) {
            return $data;
        }
        // $data is the serialized false value
        if ($data === 'b:0;') {
            return false;
        }
        // Don't attempt to unserialize data that isn't serialized
        $uns = @unserialize($data);
        // Data failed to unserialize?
        if ($uns === false) {
            $uns = @unserialize(self::fix($data));
            if ($uns === false) {
                return $data;
            } else {
                return $uns;
            }
        } else {
            return $uns;
        }
    }

Usage Example

Example #1
0
 /**
  * Execute
  * @return boolean
  */
 public static function execute(&$object, $target, $replace)
 {
     $plainObject = Ser::maybeUn($object);
     if (is_array($plainObject)) {
         $replaced = false;
         foreach ($plainObject as &$value) {
             if (static::execute($value, $target, $replace) && !$replaced) {
                 $replaced = true;
             }
         }
         $object = Ser::maybe($plainObject);
         return $replaced;
     } else {
         if (strpos($object, $target) !== false) {
             $object = str_replace($target, $replace, $object);
             return true;
         }
         return false;
     }
 }
All Usage Examples Of JBZoo\Utils\Ser::maybeUn