简体   繁体   中英

PHP: Cyrillic characters not displayed correctly

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?

尝试在标头部分放置一个表示编码的meta标签:

<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.

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?

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" />

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('á' => '&aacute;', 
                               'É' => '&Eacute;',
                               'é' => '&eacute;', 
                               'í' => '&iacute;', 
                               'û' => '&ucirc;', 
                               'ü' => '&uuml;');
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM