ruby:fixes php serialization
s:12:\"robots\\.txt$\";#!/usr/bin/env ruby
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
def fix_file file
text = File.read file
fixed = fix_serialization text
open file, 'w' do |io|
io.write fixed
end
end
def fix_serialization text
pattern = /(s\s*:\s*)(\d+)((\s*:\\*["&])(.*?)(\\?\"\s*;))/
php_escapes = /(\\r|\\n|\\t|\\v)/
text.gsub( pattern ) do |match|
front = $1
back = $3
puts $2
count = $5.length - $5.scan(php_escapes).length
"#{front}#{count}#{back}"
end
end
test_1 = fix_serialization "s:12:\"robots\\.txt$\";"
raise test_1 unless "s:12:\"robots\\.txt$\";" == test_1
fix_file 'test.sql'