flowerbloom
asked on
php postgresql convert mixed encoding to utf-8
I have user input/EDI that may be in different encoding (ISO, Win, etc.). When I insert it into the database PostgreSQL complain "invalid byte sequence for encoding "UTF8".
PostgreSQL DB encoding is UTF-8.
I cannot control the user input/EDI. The SQL query actually has mixed encoding, including UTF-8.
Is there a way to convert the none UTF-8 strings/chars to UTF-8 and ignore the strings/chars that are already UTF-8?
Thanks.
PostgreSQL DB encoding is UTF-8.
I cannot control the user input/EDI. The SQL query actually has mixed encoding, including UTF-8.
Is there a way to convert the none UTF-8 strings/chars to UTF-8 and ignore the strings/chars that are already UTF-8?
Thanks.
ASKER
Hi Ray,
Nice article. It does not help me. Let me provide an example.
$q = "inset into t1 (f1,f2,f3) values ('v1','v2','v3');";
$pg_exec($q);
Where database is PostgreSQL and has utf-8 encoding, v1 encoding is utf-8, v2 encoding is iso5589-1, and v3 encoding is win1255.
Error message: "invalid byte sequence for encoding "UTF8".
I need something like:
$q = convert_to_utf8_ignoring_a lready_utf 8($q);
$pg_exec($q);
Update successfully.
I need something like:
function convert_to_utf8_ignoring_a lready_utf 8 ($s) {
$new_s = do some magic with $s. break it apart, put it together, ignore utf-8. Make all of $s utf-8.
return $new_s;
}
Thanks.
Nice article. It does not help me. Let me provide an example.
$q = "inset into t1 (f1,f2,f3) values ('v1','v2','v3');";
$pg_exec($q);
Where database is PostgreSQL and has utf-8 encoding, v1 encoding is utf-8, v2 encoding is iso5589-1, and v3 encoding is win1255.
Error message: "invalid byte sequence for encoding "UTF8".
I need something like:
$q = convert_to_utf8_ignoring_a
$pg_exec($q);
Update successfully.
I need something like:
function convert_to_utf8_ignoring_a
$new_s = do some magic with $s. break it apart, put it together, ignore utf-8. Make all of $s utf-8.
return $new_s;
}
Thanks.
There is this function utf8_encode http://php.net/function.utf8-encode but it does not 'automatically' recognize UTF-8 strings. You have to know what you are feeding it.
ASKER
Hi Dave. This does not help. Thanks anyhow.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The perfect solution will be a function that breaks down the PostgreSQL query and go over each input fields/values and convert to UTF-8. Oh well. Thanks anyhow.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11880-Unicode-PHP-and-Character-Collisions.html