Reputation:
How in this example I can encode text from MySQL?
This is how the text look like in MySQL using utf8mb4_unicode_ci
:
$text = 'Wiesław';
I have tried so far:
header("Content-Type: text/html; charset=ISO-8859-1");
echo htmlentities($text);
echo '</br>';
echo html_entity_decode($text);
echo '</br>';
echo htmlspecialchars_decode($text);
echo '</br>';
echo htmlspecialchars_decode($text, ENT_NOQUOTES);
echo '</br>';
echo utf8_encode($text);
echo '</br>';
echo utf8_decode($text);
echo '</br>';
echo mb_convert_encoding($text, 'ISO-8859-1', 'UTF-8');
echo '</br>';
echo iconv('UTF-8', 'ISO-8859-1', $text);
echo '</br>';
echo mb_convert_encoding($text, 'UTF-8', 'ISO-8859-1');
Results:
Wies³aw
Wies³aw
Wies³aw
Wies³aw
Wies³aw
Wies?aw
Wies?aw
Wies³aw
Expectation:
Wiesław
Upvotes: 0
Views: 1166
Reputation: 142278
Grrr.. Don't use any of those functions. Instead, configure MySQL correctly.
Good: utf8mb4_unicode_ci
-- That's a "Collation"; it implies the CHARACTER SET utf8mb4
.
UTF-8
characters in the client. (Obviously, you do since you have a stoke-l).utf8mb4
. It is best to do this when establishing the connection. PHP has mysqli
and PDO
. Details for each: http://mysql.rjweb.org/doc.php/charcoll#phpCHARACTER SET utf8mb4
. Let's see SHOW CREATE TABLE
to see if that is OK now.If after doing that, you get
Wiesław -- Mojibake
Wies?aw -- question mark
Wies -- truncation
Wies�aw -- black diamond
Then go to Trouble with UTF-8 characters; what I see is not what I stored for further advice and debugging.
The hex should be 57 69 65 73 C582 61 77
.
If you get 57 69 65 73 C385 E2809A 61 77
, then you have "double encoding".
Upvotes: 1
Reputation: 860
If you're text is coming in HTML format, the following command should work:
mb_convert_encoding($text, 'HTML-ENTITIES', 'UTF-8'));
OR
mb_convert_encoding($html, 'HTML-ENTITIES', 'ISO-8859-1'));
Upvotes: 0