6

’ is being displayed instead of - in php page

I tried using different encoding types like:

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>

and

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

but result is the same. What could be the problem?

Input

<strong style="color:#A8A8A8;">1</strong> – Lorem Ipsum.

Result

1 – Lorem Ipsum.

CSᵠ
  • 10,049
  • 9
  • 41
  • 64
jeevan
  • 165
  • 2
  • 4
  • 7
  • If you don't get anything different with the two meta-tags, then your HTML might be broken somewhere. If ISO-8859-1 instead gives you `–` instead of `-`, then you have double-encoded the string – ANisus Apr 04 '13 at 11:00
  • If the server is sending a real HTTP Content-Type header with charset, then the meta tag has no effect. – Esailija Apr 04 '13 at 11:22
  • Related: [UTF-8 all the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through) (`` tags are the most irrelevant part IMHO). – Álvaro González Apr 26 '13 at 12:58

6 Answers6

5

Make sure your html header specifies utf8

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

That usually does the trick for me (obviously if the content IS utf8).

You don't need to convert to html entities if you set the content-type.


check http://php.net/manual/en/function.mb-convert-encoding.php

<?php
     header('Content-Type: text/html; charset=utf-8');
     mb_internal_encoding('utf-8');
?>

may be this will help you.

Tony Stark
  • 8,064
  • 8
  • 44
  • 63
2

It looks like your source data is converted from one to another encoding along the way. Try to make sure ALL steps have the same encoding.

  1. Is your (MySQL?) data stored as UTF8?
  2. Is your .php file saved as UTF8?

Conversion errors like this usually pop up when handling UTF8 data as ISO-8859-1 data. (multibyte vs singlebyte? not sure).

Daan
  • 191
  • 10
2

The fact that the meta tag doesn't change the output is a strong indicator that there's something overriding it; probably it's the charset specified in the HTTP header (which has precedence over the meta tag), are you sure you're not setting it there?

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
1

Your document is most likely encoded in UTF-8 since – is the iso-8859-1 presentation of the UTF-8 encoded character .

What you need is the meta-tag you describe:

<meta http-equiv="Content-Type" content="text/html;charset=utf-8">

Since it isn't working, the tag might to be ignored. Suggestion is to use the browser and check what encoding it tries to use (Tools - Encoding in Chrome).

  • If the browser uses UTF-8, you have double-encoded the characters. Check your code if so that you don't have an excessive utf8_encode(...)
  • If the browser uses Latin1 (iso-8859-1) your tag is ignored or overridden by the HTTP header. Try to validate your HTML with an online validator. Check the sent header information with your browser's development tool to make sure iso-8859-1 is not set as encoding.
ANisus
  • 74,460
  • 29
  • 162
  • 158
  • That `utf8_encode()` got me. Seems like I added it to try to fix the issue, but fixed it by setting the correct meta (as above) and removing that line of code. Thanks :) – Casey Dwayne Jan 13 '14 at 20:29
-1

Had the same problem when creating a file from javacode and setting the encoding to UTF-16 did the trick.

user3711421
  • 1,658
  • 3
  • 20
  • 37
-1

Add the following header tag:

<meta charset="UTF-8">
ZaxLofful
  • 1,034
  • 9
  • 18
Learner
  • 1
  • 1