且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用PHP FreeTDS扩展从MSSQL服务器获取UTF-8字符集的数据

更新时间:2023-11-27 17:01:46

MSSQL和UTF-8有时会很痛苦.我不得不手动将其转换. 问题是:MSSQL实际上并不知道并不支持UTF-8.

MSSQL and UTF-8 are quite a pain in the ... sometimes. I had to convert it manually. The problem: MSSQL doesn't actually know and support UTF-8.

从数据库值转换为UTF-8:

Convert from database value to UTF-8:

mb_detect_encoding($value, mb_detect_order(), true) === 'UTF-8' ? $value : mb_convert_encoding($value, 'UTF-8');

从UTF-8转换为数据库值:

Converting from UTF-8 to database value:

mb_convert_encoding($value, 'UCS-2LE', mb_detect_encoding($value, mb_detect_order(), true));

幸运的是,我正在使用Doctrine,所以我所要做的就是创建一个自定义StringType实现.

Fortunately I was using Doctrine so all I had was to create a custom StringType implementation.