0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Ruby で PHP の unserialize 関数で unserialize できる文字列を生成する

Last updated at Posted at 2025-04-30

はじめに

Ruby Script で、 PHP の unserialize 関数を通して使用する文字列を生成したくなった。
Gem php-serialize を使うと簡単に扱えたのでそのメモ。

php-serialize について

Gemfile 等に gem 'php-serialize' を追加した後、下記のように呼び出すことができる。

example.rb
require 'php-serialize'

PHP.serialize({'foo' => 'bar'})               #=> "a:1:{s:3:\"foo\";s:3:\"bar\";}"
PHP.unserialize('a:1:{s:3:"foo";s:3:"bar";}') #=> {"foo"=>"bar"}

さいごに なぜ php-serialize が必要だったか

WordPress の記事等コンテンツのエクスポートファイルである WXR (WordPress eXtended RSS) ファイルの生成に Ruby Script を使用した。
WordPress は PHP で動作する都合上 多くの場所で serialize, unserialize を実行している。

serialize 対象のデータは、実行内容によって変化するような内容であったため、動的に serialize できるようにする必要があった。

Ref

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?