PHP uft8_encode / utf8_decode with double quotation problem

364 Views Asked by At

I have a problem with utf8_encode / utf8_decode PHP function.This function must be complementary.

I know that:

  • utf8_decode convert UTF8 string to ISO-8859-1
  • utf8_encode convert ISO-8859-1 to UTF8

But if I try to convert and return back the character LEFT DOUBLE QUOTATION MARK the behaviour is different. I expect that this two echo print the same character, but this not occurs.

<?php
$doubleQuote = hex2bin("e2809c");
var_dump($doubleQuote);
$doubleQuoteNew = utf8_encode(utf8_decode($doubleQuote));
var_dump($doubleQuoteNew);

this is the output

/tmp/test.php:3:
string(3) "“"
/tmp/test.php:5:
string(1) "?"

Instead using simple character the two echo return the same. I try with php version 5 and 7.

Where is my wrong ??

1

There are 1 best solutions below

0
On

The Latin-1 encoding does not contain any "fancy quote" character. There's no equivalent character that the Unicode character could be converted to in Latin-1, so it gets replaced with a ?. That in turn can perfectly be converted back to UTF-8.