2

Is there a way to encode any string with any encoding into UTF-8?

Is there some native function in PHP?

Or is there some function free to use?

I had the problem that if I utf8_encode() a UTF-8 encoded string I get some double encoded thing which is useless.

I need to encode any incoming string, if it's Unicode, UTF-16, ISO..something, etc... into simple UTF-8. (Well, it would be good, if the function detects whether it's a string or not before manipulating it.)

Domenic
  • 110,262
  • 41
  • 219
  • 271
helle
  • 11,183
  • 9
  • 56
  • 83
  • I've got a big question going about this right now... http://stackoverflow.com/questions/3715264/how-to-handle-user-input-of-invalid-utf-8-characters – philfreo Sep 23 '10 at 16:46

1 Answers1

0
function as_utf8($s)
{
    return mb_convert_encoding($s, "UTF-8", mb_detect_encoding($s));
}
Domenic
  • 110,262
  • 41
  • 219
  • 271
  • so if my input string $s is utf-8 encoded and has some umlauts for example, and i encode it again ... i get a useless output, where my special characters are not readable anymore. isn't it? because the escapecharacters for it are escaped again ... – helle Sep 23 '10 at 16:46
  • Would you mind contributing on http://stackoverflow.com/questions/3715264/how-to-handle-user-input-of-invalid-utf-8-characters (existing related question) as well? – philfreo Sep 23 '10 at 16:46
  • Did you see the check I added that if it's UTF-8 already you don't encode it? But whatever, I since updated the code to convert from any encoding to any other encoding; if you convert from UTF-8 to UTF-8, `mb_convert_encoding` won't do anything. – Domenic Sep 23 '10 at 16:47
  • @Domenic ah, i see. seems to be a resolution, have to check that. thanks so far – helle Sep 23 '10 at 17:05
  • what would you say will happend if i process a number by it? – helle Sep 23 '10 at 17:12
  • I'd really suggest testing before asking, since it takes very little of your time, but here's your answer: PHP's built-in number-to-string conversion abilities will take over. – Domenic Sep 23 '10 at 20:02
  • :-) thanks ... testing is not so easy in my case. but this solution seems to work. – helle Sep 24 '10 at 13:35