PHP code example of rundiz / serializer

1. Go to this page and download the library: Download rundiz/serializer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rundiz / serializer example snippets


$Serializer = new \Rundiz\Serializer\Serializer();

$serialized_string = 's:54:"this is a string. สตริงภาษาไทย";';
$faked_serialized_string = 's:12:"fake serialized string";';

var_dump($Serializer->isSerialized($serialized_string);// true
var_dump($Serializer->isSerialized($faked_serialized_string);// false

$serialized_string = 's:54:"this is a string. สตริงภาษาไทย";';

var_dump(\Rundiz\Serializer\SerializerStatic::isSerialized($serialized_string));// true

$raw_data = array('mango', 'tree' => array('mango', 'banana'));

echo $Serializer->maybeSerialize($raw_data);// a:2:{i:0;s:5:"mango";s:4:"tree";a:2:{i:0;s:5:"mango";i:1;s:6:"banana";}}
echo \Rundiz\Serializer\SerializerStatic::maybeSerialize($raw_data);// a:2:{i:0;s:5:"mango";s:4:"tree";a:2:{i:0;s:5:"mango";i:1;s:6:"banana";}}

$serialized_data = 'i:-5436;';

echo $Serializer->maybeUnserialize($serialized_data);// -5436 (integer)
echo \Rundiz\Serializer\SerializerStatic::maybeUnserialize($serialized_data);// -5436 (integer)
echo \Rundiz\Serializer\SerializerStatic::maybeUnserialize(-5436);// -5436 (integer)

var_dump($Serializer->isJSONEncoded('"Hello world"'));// true
var_dump($Serializer->isJSONEncoded('012345'));// false
var_dump($Serializer->isJSONEncoded('true'));// true
var_dump($Serializer->isJSONEncoded('false'));// true
var_dump($Serializer->isJSONEncoded('null'));// true
var_dump($Serializer->isJSONEncoded(true));// true
var_dump($Serializer->isJSONEncoded(false));// false
var_dump($Serializer->isJSONEncoded(null));// false

var_dump($Serializer->isBase64Encoded(5555));// false
var_dump($Serializer->isBase64Encoded('NTU1NQ=='));// true
var_dump($Serializer->isBase64Encoded('test'));// false
var_dump($Serializer->isBase64Encoded('dGVzdA=='));// true
var_dump($Serializer->isBase64Encoded(null));// false