I need to store the column name from csv file into mysql DB, In PHP i have used to convert the string to utf8_encode format. it does not work for some string,
$name = "Téléphone"
echo utf8_encode($name); // it works
output : Téléphone
$name = "Etat œuvre"
echo utf8_encode($name); // it does not works
output : Etat Å?uvre
For testing:
echo bin2hex($name);
output: 45746174209c75767265
45746174209c75767265
The "œ" in here is 9c
, which points to the string being encoded in Windows-1252 . utf8_encode
converts from ISO-8859-1 to UTF-8, so you're doing the wrong encoding conversion. The correct one is:
mb_convert_encoding('Etat œuvre', 'UTF-8', 'CP1252')
utf8_encode
is not the "right tool" for this task. Try mb_convert_encoding
instead.
Example:
function encodeUtf8(string $data): string
{
if (!mb_check_encoding($data, 'UTF-8')) {
return mb_convert_encoding($data, 'UTF-8');
}
return $data;
}
echo encodeUtf8('Téléphone'); // Téléphone
echo encodeUtf8('Etat œuvre'); // Etat œuvre
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.