doutian3269 2015-11-25 00:20
浏览 217
已采纳

UTF-8非常奇怪的行为[重复]

This question already has an answer here:

How can I echo a sql text field into a paragraph?

My code does puts the text in but changes the accents like á to this -> . I tried adding UTF-8 in the header and removing it.

Removing UTF-A makes the sql content is ok but all the content outside the paragraph messes up.

I checked the DB was using (UTF-8-unicode), The files were saved with UTF-8

Any ideas on what might be wrong?

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

<textarea class="texto" name="textoarticulo"  id="textoarticulo" form="formarticulo" placeholder="Texto del art&iacute;culo...">
 <?
    echo $row['txt4'];

 ?>
</textarea>
</div>
  • 写回答

1条回答 默认 最新

  • douchenhui5569 2015-11-25 00:28
    关注

    It's important that your entire code has the same charset to avoid issues where characters displays incorrectly.

    Here's a little list of things that has to be set to a specific charset.

    Headers

    • Setting the charset in both HTML and PHP headers to UTF-8

      • PHP (PHP headers has to be placed before any output: PHP echo, whitespace, HTML!):

        header('Content-Type: text/html; charset=utf-8');
        
      • HTML (HTML-headers are placed within the <head> / </head> tag):

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

    Connection

    • You also need to specify the charset in the connection itself.

      • PDO (specified in the object itself):

        $handler = new PDO('mysql:host=localhost;dbname=database;charset=utf8', 'username', 'password');
        
      • MySQLi: (placed directly after creating the connection, $mysqli is the connection object)

        $mysqli->set_charset("utf8"); // OOP style
        mysqli_set_charset($mysqli, "utf8"); // procedural style
        
      • MySQL (depricated): (placed directly after creating the connection)

        mysql_set_charset("utf8");
        

    Database

    • Your database and its tables has to be set to UTF-8. Note that charset is not the same as collation.

      You can do that by running the queries below once for each database and tables (for example in phpMyAdmin)

      ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
      ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
      

      Caution... There are various different situations that need different ALTERs. Details Here . Doing the wrong ALTER is likely to make things worse. -- Rick James

    php.ini specification

    • In your php.ini file, you should specify the default charset for your platform, like this

      default_charset = "utf-8";
      

      (This is in essence the same as doing header('Content-Type: text/html; charset=utf-8'); on all pages)

    File-encoding

    • It's also important that the .php file itself is UTF-8 encoded. If you're using Notepad++ to write your code, this can be done in the "Format" drop-down on the taskbar. You should use UTF-8 w/o BOM.

    Should you follow all of the pointers above, chances are your problem will be solved. If not, you can take a look at this StackOverflow post: UTF-8 all the way through.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决