Ritesh Kumar Gupta
Ritesh Kumar Gupta

Reputation: 5191

Display Japanese Character from Encoded URL

I was doing a small task on php (Even tried the same task on cold-fusion).

There is an API that sends me URL of following Pattern as a data:

  1. http://127.0.0.1:8080/test/index.php?brand=%93%FA%8EY%8E%A9%93%AE%8E%D4

  2. http://127.0.0.1:8080/test/index.php?brand=Airtel%20India%20Mobile%20Corporation

I have to display the brand name, which is crucial part of the task. When I simply prints brand name as:

echo $_GET['brand']

, than I am getting correct output for URL 2.

URL 2 prints Airtel India Mobile Corporation.

However, I am getting wrong output for 1. URL 1 prints weird set of characters. Please Note URL-1 brand contains name of a Japanese company(Nissan Motor Co., Ltd.) in Japanese.

URL 1 : echo prints

“úŽYŽ©“®ŽÔ

I realized its an encoding problem. Then What I did is:

Right-Clicked on IE->Selected Encoding-> Selected Japanese(Shift_JIS) as Encoding.

As soon as I selected Japanese(Shift_JIS), I got the correct output.

日産自動車

How can I do it using PHP (and/or) Cold-Fusion? In other words, how can I display correct/desired o/p by PHP( or coldfusion) code (script).

Any help is appreciated.

Upvotes: 1

Views: 5963

Answers (2)

crzyonez777
crzyonez777

Reputation: 1807

I think you can use this function.

https://www.php.net/manual/ja/function.mb-convert-encoding.php

$str = $_GET["brand"]; //%93%FA%8EY%8E%A9%93%AE%8E%D4
$str = mb_convert_encoding($str, "SJIS", "auto");
echo $str; //日産自動車

EDITED

$str = $_GET["brand"]; //%93%FA%8EY%8E%A9%93%AE%8E%D4
$str = urldecode($str);
echo $str; //日産自動車

This should work.

There is "urlencode" function that do the opposite thing of "urldecode" function as well.

Upvotes: 4

silkfire
silkfire

Reputation: 25965

If this is used on a proper website, you should add that encoding to the charset attribute of your HTML header. If it's just a test page that echoes out that string then the browser will just use default encoding.

<meta charset="shift_js">

Upvotes: 1

Related Questions