2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ProjectNami で Unicode 文字列を扱う

Last updated at Posted at 2016-12-15

Azure SQL Database/SQL Server で WordPress を動作させるソリューションに ProjectNami があります。

標準の状態で日本語を扱うには、サーバーの照合順序を Japanese_CI_AS にする必要があります。
しかし、この状態ですと、cp932 の文字集合の範囲外の文字列は文字化けしてしまいます。

スマートフォンでよく使用される絵文字が文字化けしてしまうため、巷では、寿司ビール問題 と呼ばれています。これ防ぐには、 Nプレフィックスを付与する 必要があります。

以下のような修正で、 UTF-8 でのデータベース接続が有効になり、照合順序を SQL_Latin1_General_CP1_CI_AS としても文字化けせずに投稿できます。
もちろん、絵文字など、特殊な文字列の使用も可能です。

utf-8.patch
diff --git a/wp-config.php b/wp-config.php
index a1a9449..7382888 100644
--- a/wp-config.php
+++ b/wp-config.php
@@ -36,7 +36,8 @@ define('DB_HOST', ( getenv('ProjectNami.DBHost') ? getenv('ProjectNami.DBHost')
 define('DB_CHARSET', 'utf8');

 /** The Database Collate type. Don't change this if in doubt. */
-define('DB_COLLATE', '');
+define('DB_COLLATE', 'SQL_Latin1_General_CP1_CI_AS');
+putenv('ProjectNami.UTF8=1');

 /**#@+
  * Authentication Unique Keys and Salts.
diff --git a/wp-includes/wp-db.php b/wp-includes/wp-db.php
index 409d4a8..afa56ff 100644
--- a/wp-includes/wp-db.php
+++ b/wp-includes/wp-db.php
@@ -1290,7 +1290,7 @@ class wpdb {
                $query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it
                $query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting
                $query = preg_replace( '|(?<!%)%f|' , '%F', $query ); // Force floats to be locale unaware
-               $query = preg_replace( '|(?<!%)%s|', "'%s'", $query ); // quote the strings, avoiding escaped strings like %%s
+               $query = preg_replace( '|(?<!%)%s|', "N'%s'", $query ); // quote the strings, avoiding escaped strings like %%s
                array_walk( $args, array( $this, 'escape_by_ref' ) );
                return @vsprintf( $query, $args );
        }

See Also

2
4
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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?