如何在PHP中打印ñ

时间:2010-11-19 06:07:11

标签: php database string character-encoding special-characters

在数据库中,有一个名称包含ñ(exNiño)

当我从数据库中检索它然后回显它时,输出将是Ni o。

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:3)

您必须确保您的数据和您输出的文档(通过PHP)具有相同的编码。

例如,如果数据库中的数据是UTF8(并且没有损坏)。您应该确保在文档的标题中设置UTF-8。像这样:

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

(请务必在输出任何内容之前设置标题。)

有时您必须告诉您的数据库您使用的是特定的字符集:

$db->query("SET NAMES 'utf8'");
$db->query("SET CHARACTER SET utf8");

This article描述了如何确保PHP和DB从头到尾说出相同的“语言”。它需要您使用mb *方法而不是常规方法。

我建议使用一个库来处理这些事情 - 比如Flourishlib可以处理所有混乱的东西 - 数据库,标题等。

最后,请查看this general UTF 8 article,了解问题的一些见解。

答案 1 :(得分:3)

您检查了数据库配置吗?首先在您的数据库上检查它。我之前有同样的问题,当我使用cakephp时......这就是cakephp中的样子

//cakephp database configuration

<?php
/**
 1. This is core configuration file.
 2.  3. Use it to configure core behaviour ofCake.
 4.  5. PHP versions 4 and 5
 6.  7. CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 8. Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 9.  10. Licensed under The MIT License
 11. Redistributions of files must retain the above copyright notice.
 12.  13. @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 14. @link          http://cakephp.org CakePHP(tm) Project
 15. @package       cake
 16. @subpackage    cake.app.config
 17. @since         CakePHP(tm) v 0.2.9
 18. @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
/**
 19. In this file you set up your database connection details.
 20.  21. @package       cake
 22. @subpackage    cake.config
 */
/**
 23. Database configuration class.
 24. You can specify multiple configurations for production, development and testing.
 25.  26. driver => The name of a supported driver; valid options are as follows:
 27.    mysql       - MySQL 4 & 5,
 28.    mysqli      - MySQL 4 & 5 Improved Interface (PHP5 only),
 29.    sqlite      - SQLite (PHP5 only),
 30.    postgres    - PostgreSQL 7 and higher,
 31.    mssql       - Microsoft SQL Server 2000 and higher,
 32.    db2         - IBM DB2, Cloudscape, and Apache Derby (http://php.net/ibm-db2)
 33.    oracle      - Oracle 8 and higher
 34.    firebird    - Firebird/Interbase
 35.    sybase      - Sybase ASE
 36.    adodb-[drivername]  - ADOdb interface wrapper (see below),
 37.    odbc        - ODBC DBO driver
 38.  39. You can add custom database drivers (or override existing drivers) by adding the
 40. appropriate file to app/models/datasources/dbo.  Drivers should be named 'dbo_x.php',
 41. where 'x' is the name of the database.
 42.  43. persistent => true / false
 44. Determines whether or not the database should use a persistent connection
 45.  46. connect =>
 47. ADOdb set the connect to one of these
 48. (http://phplens.com/adodb/supported.databases.html) and
 49. append it '|p' for persistent connection. (mssql|p for example, or just mssql for not persistent)
 50. For all other databases, this setting is deprecated.
 51.  52. host =>
 53. the host you connect to the database.  To add a socket or port number, use 'port' => #
 54.  55. prefix =>
 56. Uses the given prefix for all the tables in this database.  This setting can be overridden
 57. on a per-table basis with the Model::$tablePrefix property.
 58.  59. schema =>
 60. For Postgres and DB2, specifies which schema you would like to use the tables in. Postgres defaults to
 61. 'public', DB2 defaults to empty.
 62.  63. encoding =>
 64. For MySQL, MySQLi, Postgres and DB2, specifies the character encoding to use when connecting to the
 65. database.  Uses database default.
 66.  */
class DATABASE_CONFIG {

var $default = array(
    'driver' => 'mysql',
    'persistent' => false,
    'host' => 'localhost',
    'login' => 'root',
    'password' => '',
    'database' => 'web_user',
    'prefix' => '',
    //'encoding' => 'utf8',
);

var $test = array(
    'driver' => 'mysql',
    'persistent' => false,
    'host' => 'localhost',
    'login' => 'user',
    'password' => 'password',
    'database' => 'test_database_name',
    'prefix' => '',
    //'encoding' => 'utf8',
);
}

您应取消注释 encoding =&gt;'utf8'

的行

答案 2 :(得分:-1)

尝试字符串替换功能(str_replace(find,replace,string)) 尝试访问此站点以获取更多信息。 http://www.w3schools.com/php/func_string_str_replace.asp 祝好运!希望这对你有所帮助! : - )