0

I have a database table and two columns are being saved differently.

Table

id     title             description
-----------------------------------------------------------
1      Test - ñ          <p>ñå</p>

As you could see the title was being saved as is and no encoding is being done. The description however is saved as an encoded string (it stands for <p>ñå</p>).

I'm having a bit of trouble displaying them correctly on a page.

Here are my approaches so far:

Title

  1. echo $data['title']; - doesn't work and displays as Test - ñ.
  2. echo html_entity_decode($data['title'], ENT_QUOTES, "UTF-8"); - still doesn't work and displays as Test - ñ.
  3. echo html_entity_decode($data['title'], ENT_XML1, "UTF-8"); - still doesn't work and displays as Test - ñ.

Description

  1. echo $data['description']; - just shows <p>ñå</p>.
  2. echo html_entity_decode($data['description'], ENT_QUOTES, "UTF-8"); - doesn't work and just displays Test - ñ.
  3. echo html_entity_decode($data['description'], ENT_XML1, "UTF-8"); - THIS SEEMS TO WORK although I'm not sure why setting ENT_XML1 fixes the problem with the description but not on the title.

Anyways, I'm not really knowledgeable about character encoding and HTML entities and special characters. Could you help me solve this problem so that both the title and description displays well. I'm not sure I'm doing the proper steps here but I was just trying different combinations of solutions.

dokgu
  • 4,957
  • 3
  • 39
  • 77
  • 1
    Just guessing : http://stackoverflow.com/questions/279170/utf-8-all-the-way-through/279279 – CD001 Jun 21 '16 at 15:25
  • have you tried using `html_entities` as that will convert **all** applicable HTML characters to their references? – Martin Jun 21 '16 at 15:25
  • If you're outputting UTF-8 (which you should be according to your HTML-decoding calls) but it's not showing up correctly in the browser, that's because you haven't told the browser to treat the page as UTF-8. See [Handling Unicode Front To Back In A Web App](http://kunststube.net/frontback/). – deceze Jun 21 '16 at 15:27
  • Can you tell us about : the encodage of your table ? The encodage of your data right before you save them ? The encodage of your web page ? – Anwar Jun 21 '16 at 15:27
  • I'd be more worried why you have such a colorful mix of encodings in your database in the first place... – deceze Jun 21 '16 at 15:28
  • And the reason why `ENT_XML1` works is simple if you look at the produced output: `

    ñå

    `.
    – deceze Jun 21 '16 at 15:29
  • try `echo utf8_decode($data['title']);` – Shapi Jun 21 '16 at 15:34
  • @AndreL The sad part is that this will actually likely work, but the OP won't understand why, take away the wrong lesson from this and use a terrible solution. **Please don't just toss around `utf8_decode`.** – deceze Jun 21 '16 at 15:38
  • I gess you are right about it – Shapi Jun 21 '16 at 15:39
  • @Zeratops I use this code: `htmlentities($_POST['description'], ENT_QUOTES, "UTF-8", false);` – dokgu Jun 21 '16 at 19:57

0 Answers0