siliconpi
siliconpi

Reputation: 8287

Browser displays � instead of ´

I have a PHP file which has the following text:

<div class="small_italic">This is what you´ll use</div>

On one server, it appears as:

This is what you´ll use

And on another, as:

This is what you�ll use

Why would there be a difference and what can I do to make it appear properly (as an apostrophe)?


Note to all (for future reference)

I implemented Gordon's / Gumbo's suggestion, except I implemented it on a server level rather than the application level. Note that (a) I had to restart the Apache server and more importantly, (b) I had to replace the existing "bad data" with the corrected data in the right encoding.

/etc/php.ini

default_charset = "iso-8859-1"

Upvotes: 9

Views: 2341

Answers (7)

Gordon
Gordon

Reputation: 316959

You have to make sure the content is served with the proper character set:

Either send the content with a header that includes

<?php header("Content-Type: text/html; charset=[your charset]"); ?>

or - if the HTTP charset headers don't exist - insert a <META> element into the <head>:

<meta http-equiv="Content-Type" content="text/html; charset=[your charset]" />

Like the attribute name suggests, http-equiv is the equivalent of an HTTP response header and user agents should use them in case the corresponding HTTP headers are not set.

Like Hannes already suggested in the comments to the question, you can look at the headers returned by your webserver to see which encoding it serves. There is likely a discrepancy between the two servers. So change the [your charset] part above to that of the "working" server.

For a more elaborate explanation about the why, see Gumbo's answer.

Upvotes: 16

GordonM
GordonM

Reputation: 31730

This is probably being caused by the data you're inserting into the page with PHP being in a different character encoding from the page itself (the most common iteration is one being Latin 1 and the other UTF-8).

Check the encoding being used for the page, and for your database. Chances are there will be a mismatch.

Upvotes: 0

Michel
Michel

Reputation: 11

Set your browser's character set to a defined value:

For example,

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

Upvotes: 0

Alex Pliutau
Alex Pliutau

Reputation: 21957

  1. Create an .htaccess file in the root directory:

    AddDefaultCharset utf-8
    AddCharset utf-8 *
    <IfModule mod_charset.c>
        CharsetSourceEnc utf-8
        CharsetDefault utf-8
    </IfModule>
    
  2. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

Upvotes: -1

Zain Shaikh
Zain Shaikh

Reputation: 6043

The simple solution is to use ASCII code for special characters.

The value of the apostrophe character in ASCII is &#8217;. Try putting this value in your HTML, and it should work properly for you.

Upvotes: 1

Czar
Czar

Reputation: 366

To sum it maybe up a little bit:

  1. Make sure the FILE saved on the web server has the right encoding
  2. Make sure the web server also delivers it with the right encoding
  3. Make sure the HTML meta tags is set to the right encoding
  4. Make sure to use "standard" special chars, i.e. use the ' instead of ´of you want to write something like "Luke Skywalker's code"

For encoding, UTF-8 might be good for you.

If this answer helps, please mark as correct or vote for it. THX

Upvotes: 1

Gumbo
Gumbo

Reputation: 655189

The display of the REPLACEMENT CHARACTER (U+FFFD) most likely means that you’re specifying your output to be Unicode but your data isn’t.

In this case, if the ACUTE ACCENT ´ is for example encoded using ISO 8859-1, it’s encoded with the byte sequence 0xB4 as that’s the code point of that character in ISO 8859-1. But that byte sequence is illegal in a Unicode encoding like UTF-8. In that case the replacement character U+FFFD is shown.

So to fix this, make sure that you’re specifying the character encoding properly according to your actual one (or vice versa).

Upvotes: 8

Related Questions