7

I want the string Game to convert Game. This string is a Japanese double byte string.

Is it possible to achieve this using PHP? If so, how?

5
  • Use mb_convert_encoding() with the appropriate encodings Commented Mar 22, 2014 at 10:21
  • @MarkBaker : i would try this mb_convert_encoding() for php but in mysql part... is there any function for this ? because my probable solution would be base on the query directly.. Commented Mar 22, 2014 at 11:21
  • If you need to do this in MySQL, then use CONVERT() or CAST() Commented Mar 22, 2014 at 11:28
  • What encoding? UTF-16? Commented Apr 10, 2014 at 4:27
  • @tadman: this is more likely to be UTF8 => SJIS-win in PHP. or utf8 => sjis(Shift-JIS Japanese) in MySQL Commented Apr 10, 2014 at 10:11

3 Answers 3

5
+50

First of all, Game is not ASCII charset, so you might need to set the page Content-type header to see a proper output:

header("Content-type: text/html; charset=utf-8");

Then you can convert it using this function

echo mb_convert_kana('Game', "R", 'UTF-8')

EDIT:

For MySQL I couldn't find a converter to do the same. However, you can still convert it manually on the Hex level, for example, you can get the word Game like this

SELECT CHAR(0xefbca7, 0xefbd81, 0xefbd8d, 0xefbd85) as `Full Width`;

So, we can just write a mapping function in MySQL that replaces the characters using this table

Sign up to request clarification or add additional context in comments.

1 Comment

@KraneBird: thanks for your answer in php part... but how about in mysql part, is there any way to achieve this ?
0

Well, can't say for sure whether I got your question right, but the following console one-liner:

$ php -r 'var_dump(mb_convert_encoding("Game", "UCS-2"));'

gives me the following:

string(8) "\000G\000a\000m\000e"

Is this what you want? As Mark Baker already said, it's just the case of mb_convert_encoding for you.

3 Comments

How would you determine which encoding to use? Using mb_detect_encoding perhaps?
Man, but you are going to convert to the multibyte encoding! I assume that you have control over the input string, and you know to what encoding you want to convert! Use Shift-JIS, for example, or EUC. I have used UCS-2 because it's guaranteed to be exactly two-byte Unicode encoding, as you have specified.
This doesn't still convert Game => Game.
-1

you can try for this function mb_convert_kana

1 Comment

this is in contrast of what i've wanted to be converted. i want this kind of conversion 'Game' => 'Game'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.