Recently 开发者_如何学运维I switched hosting from one provider to the other and I have problems displaying Cyrillic characters. The characters which are read from the database are displayed correctly, but characters which are hardcoded in the php file aren't (they are displayed as question marks).
The files which contain the php source code are saved in utf-8 form. Help anybody?
Try placing a meta tag indicating the encoding in the head section:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
My PHP module was exactly with same problem all text was like "?????????????????"
And my code was written with Notepad++. I found the solution for the problem. The problem wasn't in the header charset or meta tag because the browser actually knows that it is the UTF-8 charset. I tried all encodings from the browser and the result was the same so I knew the problem is somewhere else, not in the browser character encoding at all.
I just opened the PHP module with Notepad++ and selected all code. After that in the Encoding menu I selected "Convert to UTF-8." After uploading to the server, everything worked like a charm.
put this after connecting database:
mysql_query("SET NAMES UTF8");
for mysqli
mysqli_query($connecDB,"SET NAMES UTF8");
and in the header of the page:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
The problem seems quite strange.
What's the form of these question marks? Is it black diamonds with questions or just plain question marks?
First of all double check if your files are really utf-8
encoded.
Try to add this header to your code (above all output)
header('Content-Type: text/html; charset=utf-8');
But I doubt it would help, as your database text already looks good.
Do you have any SET NAMES
queries in your code? What charset
it is set?
It had something to do with the encoding of the php files. The files were created using Windows Notepad and saved with utf-8 encoding.
When I used Notepad2 to open the files, the encoding of the files was "utf-8 with signature". When I changed encoding to "utf-8", the text displayed correctly.
The reason for your problem is often accidental re-encoding the script files by a programmer's editor. It isn't a good practice to hardcode strings which rely on encoding in your php files.
Try switching your browser's encoding to find what encoding is used for hardcoded text, it might help you address the issue. Also make sure to send proper http headers for each page:
header('Content-Type: text/html; charset=utf-8');
Optionaly you can insert meta tag in you HTML:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
For me, the line that made the difference is the following:
$mysqli->set_charset("utf8")
Straight from the PHP documentation page.
The server was returning latin, after setting the charset to utf8 now works fine.
I have been fighting with this exact same problem as I'm trying to add a bit of french/german internationalization to a few controls on a widget.
Characters with accents that are stored in my db print fine as UTF-8. However, characters that are hardcoded into arrays in PHP files either display as the black diamond with a question mark inside or the little square box.
I've tried encoding/decoding the hardcoded strings from my php file every which way, but couldn't get the characters to display properly.
Since I have such a finite set of characters and am working strictly with HTML, I just added a bit of functionality to my intl class to substitute the characters for html entities.
I have these properties.
static $accentEntities = array('á' => 'á',
'É' => 'É',
'é' => 'é',
'í' => 'í',
'û' => 'û',
'ü' => 'ü');
static $accents = array();
static $entities = array();
I setup some my replacement arrays in my constructor...
foreach (self::$accentEntities as $char => $entity) {
self::$accents[] = $char;
self::$entities[] = $entity;
}
And then when I need one of my hardcoded strings in my class I just return it like so...
return str_replace(self::$accents,self::$entities,$str);
It's a totally ghetto solution... but for now, it works. I'd definitely like to hear the correct way to display accents/special characters that are hardcoded into a PHP file.
精彩评论