非常奇怪的行为与UTF-8 [英] Very strange behaviour with UTF-8

查看:117
本文介绍了非常奇怪的行为与UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将sql文本字段回显到段落?

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

我的代码将文本放入,但将重音符更改为á至此 - >
我尝试在标题中添加UTF-8并将其删除。

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.

删除UTF-A使sql内容正常,但段落之外的所有内容都会混乱。

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

我检查了DB正在使用(UTF-8-unicode),这些文件是用UTF-8保存的

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>


推荐答案

重要的是,您的整个代码具有相同的字符集,以避免字符显示不正确的问题。

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.

标题


  • 设置HTML和PHP标头到UTF-8

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


  • PHP( PHP标头必须放在任何输出:PHP echo,空格,HTML!

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

header('Content-Type: text/html; charset=utf-8');


  • HTML (HTML标题放在 < head> / < / head> 标签)

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


  • 连接


    • 您还需要在连接本身中指定字符集

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


    • PDO(在对象本身中指定):

    • PDO (specified in the object itself):

    $handler = new PDO('mysql:host=localhost;dbname=database;charset=utf8', 'username', 'password');
    


  • MySQLi :(直接创建连接后, $ mysqli 是连接对象)

    $mysqli->set_charset("utf8"); // OOP style
    mysqli_set_charset($mysqli, "utf8"); // procedural style
    


  • MySQL( depricated ):(放置直接创建连接后)

  • MySQL (depricated): (placed directly after creating the connection)

    mysql_set_charset("utf8");
    


  • 数据库


    • 您的数据库及其表必须设置为UTF-8。请注意,字符集不是与排序规则相同。

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

    您可以通过对每个数据库和表运行以下查询一次(例如在phpMyAdmin中)

    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;
    

    注意... 有各种不同的情况需要不同的改变 详细信息 。执行错误的 ALTER 可能会使事情变得更糟。 - Rick James

    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规范


    • 在您的 php.ini 文件中,您应该为您的平台指定默认字符集, / p>

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

    default_charset = "utf-8";
    

    (这实际上和做 header('内容类型:text / html; charset = utf-8'); 在所有页面)

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

    文件编码


    • 同样重要的是, .php 文件本身是UTF-8编码的。如果您使用Notepad ++编写代码,可以在任务栏上的格式下拉列表中完成。你应该使用UTF-8 w / o BOM。

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

    如果你遵循上面的所有指针,你的问题可能会被解决。如果没有,您可以查看这个StackOverflow文章: UTF-8一直通过

    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.

    这篇关于非常奇怪的行为与UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆