PHP code example of satooshi / ltsv-encoder

1. Go to this page and download the library: Download satooshi/ltsv-encoder 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/ */

    

satooshi / ltsv-encoder example snippets






use Contrib\Component\Serializer\Factory;

// deserialize
$str = "label1:value1\tlabel2:value2";
$serializer = Factory::createSerializer();
$data = $serializer->decode($str, 'ltsv');


// $data
[
  'label1' => "value1",
  'label2' => "value2",
]



use Contrib\Component\Serializer\Factory;

// encode
$serializer = Factory::createSerializer();
$str = $serializer->encode($data, 'ltsv');

// $str
"label1:value1\tlabel2:value2"



use Contrib\Component\Serializer\Factory;

// encode
$data = new SerializableEntity(array('id' => 1, 'name' => 'hoge'));
$serializer = Factory::createSerializer();
$str = $serializer->serialize($data, 'ltsv');

// $str
"id:1\tname:hoge"



use Contrib\Component\Serializer\Factory;

// deserialize
$str = "id:1\tname:hoge";
$serializer = Factory::createSerializer();
$data = $serializer->deserialize($str, 'SerializableEntity', 'ltsv');


// $data
class SerializableEntity {
  protected $id =>
  int(1)
  protected $name =>
  string(4) "hoge"
}



use Contrib\Component\Serializer\Factory;

$format = 'ltsv';

// you can change these default options
$context =
[
    'to_encoding' =>'UTF-8',
    'from_encodeing' => 'auto',
    'strict' => false,
    'store_context' => false,
];

$serializer = Factory::createSerializer();
$serializer->decode($data, $format, $context);
$serializer->encode($data, $format, $context);
$serializer->serialize($data, $format, $context);
$serializer->deserialize($data, $type, $format, $context);

// change options
$context =
[
    'strict' => true,
];

// recreate serializer object
$serializer = Factory::createSerializer();
$serializer->decode($data, $format, $context);
$serializer->encode($data, $format, $context);
$serializer->serialize($data, $format, $context);
$serializer->deserialize($data, $type, $format, $context);