phex v0.2.0 Phex View Source

A PHP serialized decoder and encoder.

Installation

The package can be installed by adding phex to your list of dependencies in mix.exs:

def deps do
[
  {:phex, "~> 0.1.0"}
]
end

Basic Usage

iex> Phex.encode!(%{"age" => 42, "name" => "John Doe"})
"a:2:{s:3:\"age\";i:42;s:4:\"name\";s:8:\"John Doe\";}"

iex> Phex.decode!("a:2:{s:3:\"age\";i:42;s:4:\"name\";s:8:\"John Doe\";}")
%{"age" => 42, "name" => "John Doe"}

PHP Serialization

Currently only maps with keys which are either integers or strings are supported.

This works in most cases but can create invalid serialized data if the peculiarities of PHP arrays are not taken into account. More information can be found in the PHP manual here.

As of now supported types are ...

iex> encode!(nil)
"N;"

iex> decode!("N;")
nil

Boolean

iex> encode!(true)
"b:1;"

iex> decode!("b:0;")
false

Integer

iex> encode!(9000)
"i:9000;"

iex> decode!("i:-212;")
-212

Float

iex> encode!(3.14159265359)
"d:3.14159265359;"

iex> decode!("d:6.62607004;")
6.62607004

String

iex> encode!("Hello")
"s:5:\"Hello\";"

iex> decode!("s:5:\"World\";")
"World"

Maps (PHP-Arrays)

iex> encode!(%{"Blue" => 2, "Pink" => 4})
"a:2:{s:4:\"Blue\";i:2;s:4:\"Pink\";i:4;}"

iex> decode!("a:2:{s:6:\"Bunker\";s:4:\"Blue\";s:7:\"Buscemi\";s:4:\"Pink\";}")
%{"Bunker" => "Blue", "Buscemi" => "Pink"}

Objects

iex> decode!(~S(O:8:"PhpClass":2:{s:21:"PhpClassaPrivateVar";s:16:"A private String";s:10:"aPublicVar";s:15:"A public String";}))
%{
  :__object__ => "PhpClass",
  "PhpClassaPrivateVar" => "A private String",
  "aPublicVar" => "A public String"
}

Link to this section Summary

Link to this section Functions