-4
$str1 = '日本の山が好きです。나는 한국사람 입니다.';
$str = iconv("UTF-8", "SJIS//TRANSLIT", $str1);
echo $str;

iconv(): Detected an illegal character in input string

IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • 1
    Can't reproduce _that_ error, getting a warning saying _'conversion from "UTF-8" to "SJIS//TRANSLIT" is not allowed'_ across all versions instead, see https://3v4l.org/AsXtA – CBroe Jun 09 '22 at 10:07
  • @CBroe `iconv` is weirdly platform-dependent, and for whatever reason the version on 3v4l is rather limited. – IMSoP Jun 09 '22 at 11:18

1 Answers1

1

I think you have a few misconceptions here:

  • "Transliterate" does not mean the same thing as "translate". One is about converting between writing systems; the other is about converting between languages.
  • A character encoding is not necessarily specific to either a writing system or a language, it is just a way of representing characters as binary data.
  • Shift-JIS is a standard primarily designed for representing characters used by Japanese text, so if this did work, it would convert from Korean text to Japanese text, not the other way around.
  • The "TRANSLIT" option in iconv doesn't promise to map every character in every writing system to some equivalent in any other writing system.

The actual meaning of that option is described in the PHP manual (I've emphasised some key words):

If the string //TRANSLIT is appended to to_encoding, then transliteration is activated. This means that when a character can't be represented in the target charset, it may be approximated through one or several similarly looking characters.

For example, if I run iconv("UTF-8", "ASCII//TRANSLIT", "é") the result is "e", because the version of iconv I have available has a "transliteration" mapping that says that "e" is "similar looking" to "é".

The error message is simply saying that even with "TRANSLIT" mode turned on, iconv can't find any way way to represent Hangul characters such as "나" using the Shift-JIS encoding. There is no direct mapping for those characters in the Shift-JIS standard, and it has no rules which define "similar looking" characters to replace them with.

IMSoP
  • 89,526
  • 13
  • 117
  • 169