Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
PHP's serialization implemenatation for ruby
branch: master

Merge pull request #4 from yasuoza/fix/multibyte_char_length

Fix serializing multibyte character length issue.
latest commit b4687307d6
Rodrigo Kochenburger authored

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.