1

I have a Japanese string in my (UTF-8 encoded) database '利用規約' which is passed to php's json_encode function and receive this warning:

PHP Warning: json_encode(): Invalid UTF-8 sequence in argument

Is there a way to convert this to valid UTF-8 in order to avoid this? If the db is utf-8 why would php have a problem with the string?

Update:

Turns out the error was in fact due to a problem with sprintf and multibyte characters a couple lines earlier.

  • are you sure you are using utf-8 to transmit strings between queries and results, using `SET NAMES utf8` ? – SirDarius Apr 20 '14 at 08:45

2 Answers2

7

utf8_encode should work for you.

http://www.php.net/manual/en/function.utf8-encode.php

If utf8_encode doesn't work, try mb_convert_encoding:

mb_convert_encoding($string,"UTF-8","auto");
Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95
  • I had tried that. Interestingly on my laptop running OS X php 5.3.20 that function returns same string '利用規約'. On the server in question (php 5.3.10) I can't even paste that string into an interective php session. I can paste it into bash shell though. could this be a php version or configuration issue? – But those new buttons though.. Apr 20 '14 at 07:48
  • I'm accepting this answer because even though it did not solve my problem, it is in fact, correct and relevant. – But those new buttons though.. Apr 20 '14 at 11:02
1

utf8_encode only works with ISO-8859-1 (see language coverage here wikip)


you should try mb_convert_encoding() php encoding doc

:)

uzerzero
  • 146
  • 2
  • 6
  • I disagree, from your question I believe it was related to encoding not config and also agreed with Devon, it was mentioned in the answer by Devon which you accepted as your answer. Therefore even if my answer was not exact, it was relevant. – uzerzero Apr 20 '14 at 11:11
  • sorry, you're correct. if you edit your answer slightly (due to system requirements) I can remove the downvote. – But those new buttons though.. Apr 20 '14 at 13:53