Take the 2-minute tour ×

I'm writing a php program that pulls from a database source. Some of the varchars have quotes that are displaying as black diamonds with a question mark in them (�, REPLACEMENT CHARACTER, I assume from Microsoft Word text).

How can I use php to strip these characters out?

share|improve this question
add comment

7 Answers

This is a charset issue. As such, it can have gone wrong on many different levels, but most likely, the strings in your database are utf-8 encoded, and you are presenting them as iso-8859-1. Or the other way around.

The proper way to fix this problem, is to get your character-sets straight. The simplest strategy, since you're using PHP, is to use iso-8859-1 throughout your application. To do this, you must ensure that:

  • All PHP source-files are saved as iso-8859-1 (Not to be confused with cp-1252).
  • Your web-server is configured to serve files with charset=iso-8859-1
  • Alternatively, you can override the webservers settings from within the PHP-document, using header.
  • In addition, you may insert a meta-tag in you HTML, that specifies the same thing, but this isn't strictly needed.
  • You may also specify the accept-charset attribute on your <form> elements.
  • Database tables are defined with encoding as latin1
  • The database connection between PHP to and database is set to latin1

If you already have data in your database, you should be aware that they are probably messed up already. If you are not already in production phase, just wipe it all and start over. Otherwise you'll have to do some data cleanup.

A note on meta-tags, since everybody misunderstands what they are:

When a web-server serves a file (A HTML-document), it sends some information, that isn't presented directly in the browser. This is known as HTTP-headers. One such header, is the Content-Type header, which specifies the mimetype of the file (Eg. text/html) as well as the encoding (aka charset). While most webservers will send a Content-Type header with charset info, it's optional. If it isn't present, the browser will instead interpret any meta-tags with http-equiv="Content-Type". It's important to realise that the meta-tag is only interpreted if the webserver doesn't send the header. In practice this means that it's only used if the page is saved to disk and then opened from there.

This page has a very good explanation of these things.

share|improve this answer
Cheers, this worked for me. –  Petrogad Jul 14 '09 at 22:11
add comment

If you see that character (� U+FFFD "REPLACEMENT CHARACTER") it usually means that the text itself is encoded in some form of single byte encoding but interpreted in one of the unicode encodings (UTF8 or UTF16).

If it were the other way around it would (usually) look something like this: Ã¤.

Probably the original encoding is ISO-8859-1, also known as Latin-1. You can check this without having to change your script: Browsers give you the option to re-interpret a page in a different encoding -- in Firefox use "View" -> "Character Encoding".

To make the browser use the correct encoding, add an HTTP header like this:

header("Content-Type: text/plain; charset=ISO-8859-1");

or put the encoding in a meta tag:


Alternatively you could try to read from the database in another encoding (UTF-8, preferably) or convert the text with iconv().

share|improve this answer
So far this is the closest solution. However, now I have a meta: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> and I'm using iconv to convert from iso-8859-1 to utf-8, the charecters now show as a box with 0096 and 0092 respectivley special(' or -) any other thoughts? –  vishnuvyas Nov 9 '08 at 1:17
yes, i have another thought: do some homework... you probably used the wrong source encoding. 0x92 and 0x96 are "curved single quote" and "dash" in windows-1252. could that be the right one? have you tried the browser-trick? –  hop Nov 9 '08 at 1:31
The PHP header fixed things for me when using the PDF2Text class. –  James Poulson Aug 15 '13 at 23:00
add comment

I also faced this issue �. I was using substr() on a UTF8 string which cut UTF8 characters, thus the cut chars could not be displayed correctly. Use mb_substr($utfstring,0,10,'utf-8'); instead. This solution, of course, was found on stackoverflow ;)

Other issue I had once was using htmlspecialchars() on a UTF8 string. Here I had to use: htmlspecialchars($utfstring, ENT_QUOTES, 'UTF-8');

Maybe this additional information will help others.

share|improve this answer
add comment

Based on your description of the problem, the data in your database is almost certainly encoded as Windows-1252, and your page is almost certainly being served as ISO-8859-1. These two character sets are equivalent except that Windows-1252 has 16 extra characters which are not present in ISO-8859-1, including left and right curly quotes.

Assuming my analysis is correct, the simplest solution is to serve your page as Windows-1252. This will work because all characters that are in ISO-8859-1 are also in Windows-1252. In PHP you can change the encoding as follows:

header('Content-Type: text/html; charset=Windows-1252');

However, you really should check what character encoding you are using in your HTML files and the contents of your database, and take care to be consistent, or convert properly where this is not possible.

share|improve this answer
The problem with this suggestion is that most likely the data is a mix of different charsets at this point. If you don't know exactly what went wrong, it just becomes even messier, if you just throw some random fixes in here and there. –  troelskn Nov 9 '08 at 12:07
I agree. I edited my post somewhat to reflect that this solution isn't a substitute for knowing what you're doing. However, I've come to the conclusion that most developers are either incapable of understanding this issue, or just don't care. It seems to come up at least once a month where I work. –  Daniel Cassidy Nov 10 '08 at 3:38
That's pretty much my observation too. For what I care, they reap as they sow. But you're probably right; Chances are that his data is indeed cp-1252 .. At least some of it is. –  troelskn Nov 10 '08 at 18:18
add comment

That can be caused by unicode or other charset mismatch. Try changing charset in your browser, in of the settings the text will look OK. Then it's question of how to convert your database contents to charset you use for displaying. (Which can actually be just adding utf-8 charset statement to your output.)

share|improve this answer
add comment

You can also change the caracter set in your browser. Just for debug reasons.

share|improve this answer
add comment

To make sure your MYSQL connection is set to UTF-8 (or latin1, depending on what you're using), you can do this to:


or use this to check what charset you are using:

$charset = mysql_client_encoding($con);
echo "The current character set is: $charset\n"; 

More info here: http://php.net/manual/en/function.mysql-set-charset.php

share|improve this answer
add comment

Your Answer

