Why does my output change?

2019-07-16 02:41发布

I'm working with UTF-8 encoding in PHP and I keep managing to get the output just as I want it. And then without anything happening with the code, the output all of a sudden changes.

Previously I was getting hebrew output. Now I'm getting "&&&&&".

Any ideas what might be causing this?

标签: php utf-8
4条回答
闹够了就滚
2楼-- · 2019-07-16 03:01

I put together this method and called it in the file I'm working with, and that seemed to resolve the issue.

function setutf_8()
    {
    header('content-type: text/html; charset: utf-8');
    mb_internal_encoding('UTF-8');
    mb_http_output('UTF-8');
    mb_http_input('UTF-8');
    mb_language('uni');
    mb_regex_encoding('UTF-8');
    ob_start('mb_output_handler');
    }

Thank you for all your help! :)

查看更多
戒情不戒烟
3楼-- · 2019-07-16 03:22

Try after setting the content type with header like this

header('Content-Type: text/html; charset=utf-8');
查看更多
虎瘦雄心在
4楼-- · 2019-07-16 03:25

Try this function - >

$html = "Bla Bla Bla...";
$html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");

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

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-07-16 03:28

These are most common problems:

  • Your editor that you’re creating the PHP/HTML files in
  • The web browser you are viewing your site through
  • Your PHP web application running on the web server
  • The MySQL database
  • Anywhere else external you’re reading/writing data from (memcached, APIs, RSS feeds, etc)

And few things you can try:

Configuring your editor

Ensure that your text editor, IDE or whatever you’re writing the PHP code in saves your files in UTF-8 format. Your FTP client, scp, SFTP client doesn’t need any special UTF-8 setting.

Making sure that web browsers know to use UTF-8

To make sure your users’ browsers all know to read/write all data as UTF-8 you can set this in two places.

The content-type tag

Ensure the content-type META header specifies UTF-8 as the character set like this:

<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">

The HTTP response headers

Make sure that the Content-Type response header also specifies UTF-8 as the character-set like this:

ini_set('default_charset', 'utf-8')

Configuring the MySQL Connection

Now you know that all of the data you’re receiving from the users is in UTF-8 format we need to configure the client connection between the PHP and the MySQL database. There’s a generic way of doing by simply executing the MySQL query:

SET NAMES utf8;

…and depending on which client/driver you’re using there are helper functions to do this more easily instead:

With the built in mysql functions

mysql_set_charset('utf8', $link);

With MySQLi

$mysqli->set_charset("utf8")

*With PDO_MySQL (as you connect)*

$pdo = new PDO( 
    'mysql:host=hostname;dbname=defaultDbName', 
    'username', 
    'password', 
    array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") 
);

The MySQL Database

We’re pretty much there now, you just need to make sure that MySQL knows to store the data in your tables as UTF-8. You can check their encoding by looking at the Collation value in the output of SHOW TABLE STATUS (in phpmyadmin this is shown in the list of tables). If your tables are not already in UTF-8 (it’s likely they’re in latin1) then you’ll need to convert them by running the following command for each table:

ALTER TABLE myTable CHARACTER SET utf8 COLLATE utf8_general_ci;

One last thing to watch out for

With all of these steps complete now your application should be free of any character set problems. There is one thing to watch out for, most of the PHP string functions are not unicode aware so for example if you run strlen() against a multi-byte character it’ll return the number of bytes in the input, not the number of characters. You can work round this by using the Multibyte String PHP extension though it’s not that common for these byte/character issues to cause problems.

Taken form here: http://webmonkeyuk.wordpress.com/2011/04/23/how-to-avoid-character-encoding-problems-in-php/

查看更多
登录 后发表回答