How to intentionally insert strings with the wrong

2019-07-17 06:22发布

This is probably one of the weirdest questions that i had to ask in Stackoverflow :)

I have a legacy, non-tested PHP application which i can't touch at all. This application uses Mysql and one database per account. So we have thousands of databases.

Due to an error that was there way before i started working on it, this app connects with the wrong encoding in Mysql. So in database, where we were supposed to have "é" we actually get "é". Although in application ( due to the wrong connection encoding ), we get é.

I have a Rails application that manage the creation of accounts/databases. The problem is that the Rails application is using the correct encoding and when it creates the database, it will insert some data which the PHP application can't read properly.

My question: considering that i have a string "é" in Ruby, how to i intentionally change it to "é"? Any string.encode that i can use?

Note 1: I can't dump all databases and fix this ( which would be the ideal solution )

Note 2: I can't switch Rails connection encoding as it uses another database which has the correct encoding

1条回答
一夜七次
2楼-- · 2019-07-17 06:45

You can try to have the default charset as

config.action_controller.default_charset = 'ISO-8859-1'

in config/application.rb

The original PHP encoding looks like to be ISO 8859-1 or Windows-1252

I tried this on a irb and found that this is working

1.9.3p194 :002 > puts "é".force_encoding("ISO-8859-1").encode("UTF-8")
é
 => nil 
查看更多
登录 后发表回答