tags:

views:

31

answers:

1

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.)

A: 
function as_utf8($s)
{
    return mb_convert_encoding($s, "UTF-8", mb_detect_encoding($s));
}
Domenic
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
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
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
@Domenic ah, i see. seems to be a resolution, have to check that. thanks so far
helle
what would you say will happend if i process a number by it?
helle
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
:-) thanks ... testing is not so easy in my case. but this solution seems to work.
helle