Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

PHP's serialization implemenatation for ruby

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 features
Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 .document
Octocat-spinner-32 .gitignore
Octocat-spinner-32 LICENSE
Octocat-spinner-32 README.rdoc
Octocat-spinner-32 Rakefile
Octocat-spinner-32 VERSION
Octocat-spinner-32 php-serialization.gemspec
README.rdoc

PhpSerialization

Ruby implementation of PHP's serialization. This is special useful for reading PHP session files.

Serialization examples

Primitive values

PhpSerialization.dump(10)       # => "i:10;"
PhpSerialization.dump("Name")   # => "s:4:\"Name\";"
PhpSerialization.dump(true)     # => "b:1;"
PhpSerialization.dump(nil)      # => "N;"

Array

PhpSerialization.dump([true, "foo"]) # => "a:2:{i:0;b:1;i:1;s:3:\"foo\";}"

Hash

PhpSerialization.dump("name" => "Rodrigo", "age" => 23) # => "a:2:{s:4:\"name\";s:7:\"Rodrigo\";s:3:\"age\";i:23;}"

Object

class Person
  attr_accessor :name, :age
end

person = Person.new
person.name = "Rodrigo"
person.age  = 23

PhpSerialization.dump(person) # => "O:6:\"Person\":2:{s:4:\"name\";s:7:\"Rodrigo\";s:3:\"age\";i:23;}"

Unserialization examples

Primitive values

PhpSerialization.load('i:10;')       # => 10
PhpSerialization.load('s:4:"Name";') # => "Name"
PhpSerialization.load('b:1;')        # => true
PhpSerialization.load('N;')          # => nil

Array

PhpSerialization.load('a:2:{i:0;b:1;i:1;s:3:"foo";}') # => [true, "foo"]

Hash

PhpSerialization.load('a:2:{s:4:"name";s:7:"Rodrigo";s:3:"age";i:23;}') # => {"name" => "Rodrigo", "age" => 23}

Object

class Person
  attr_accessor :name, :age
end

person = PhpSerialization.load('O:6:"Person":2:{s:4:"name";s:7:"Rodrigo";s:3:"age";i:23;}')
person.name # => "Rodrigo"
person.age  # => 23

Object without class will map to a Struct

person = PhpSerialization.load('O:6:"Person":2:{s:4:"name";s:7:"Rodrigo";s:3:"age";i:23;}')
person.class # => Struct::Person
person.name  # => "Rodrigo"
person.age   # => 23

You can also call unserialize() or restore(), they are alias to the load().

Copyright

Copyright © 2009 Rodrigo Kochenburger. See LICENSE for details.

Something went wrong with that request. Please try again.