John Davidson

encoding - PHP mb_convert_encoding convert from UTF-8 to SHIFT JIS is wrong

0 comments
Message:


I use mb_convert_encoding function to convert UTF8 characters to SJIS characters.


Before conversion:でんぱ組 出会いの歌26 カミソヤマ ユニ


After conversion: て?んは?組 出会いの歌26 カミソヤマ ユニ


Non-convertible characters: て?んは?


Code used to convert :


$str = mb_convert_encoding('でんぱ組 出会いの歌26 カミソヤマ ユニ', "SJIS", "UTF-8");


My Answer:
The issue you are facing is likely due to the fact that some characters in UTF-8 cannot be directly converted to SHIFT JIS encoding. In such cases, the mb_convert_encoding function may replace those characters with question marks or other placeholders.

One way to handle this issue is to use the //IGNORE flag in the mb_convert_encoding function, which will ignore any characters that cannot be converted. Here is an example of how you can modify your code to use the //IGNORE flag:

php
$str = mb_convert_encoding('でんぱ組 出会いの歌26 カミソヤマ ユニ', "SJIS", "UTF-8//IGNORE");


By adding //IGNORE at the end of the encoding parameter, the function will skip any characters that cannot be converted, instead of replacing them with question marks.

Please note that this approach may result in loss of information for characters that cannot be converted, so it's important to consider the implications for your specific use case.

Rate this post

4 of 5 based on 8230 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us