从数据库表打印文本时的字符集问题 [英] charset issues in printing text from db table

查看:116
本文介绍了从数据库表打印文本时的字符集问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用charset utf8_general_ci定义的数据库,以及一个应该存储从具有相同字符集的文本区域表单中插入的文本的表。

I have a database defined with charset utf8_general_ci and a table that should store some text inserted from a text area form with the same charset.

我使用的表单获取文本是:

The form I use to get the text is this:

<form action="submit_text.php" method="post">

    Text:</br>
    <textarea name="text" cols="109" rows="20">
        <?php echohtmlspecialchars($_POST['text']);?>
    </textarea>

    <input name="submit" type="submit" value="save text">

</form>

我用来将此文本保存在我的数据库中的php指令如下:

The php instructions I use to save this text in my database are the following:

$text = $_POST['text'];
$query = "INSERT INTO table_name VALUES (..., '$text', ...)";
$query_result = mysql_query($query) or die (mysql_error());
return $query_result;

然后我有一个页面,通过选择一个元素来打印保存在数据库表中的文本表,然后回显查询结果的文本字段(不显示查询部分):

I then have a page where I print the text saved in the database table by selecting one element of the table and then echoing the text field of the query result (not showing the query part):

<div class="entry">
    <?php echo $selected_element_of_table['text'];?>
</div>

但是,文本中的所有特殊字符都被拧紧,并且不会正确打印换行符或制表符。

However, all special characters in the text are screwed up, and neither newlines or tabs are printed correctly.

有没有人知道我的问题是什么?
应该更改字符集编码吗?

Does anybody have an idea of what my problem is? Should I change charset encoding?

提前感谢!

推荐答案

有几件事要试试:

在会显示 UTF-8内容,您的标头中包含:

In pages that will display UTF-8 content, in your header include:

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

在您的PHP中,在输出到浏览器之前,包括内容类型PHP标头

In your PHP, before any output to the browser, include the content type PHP header:

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

在运行SQL以获取内容之前,请使用 mysql_set_charset

Before you run your SQL to fetch content, use mysql_set_charset:

mysql_set_charset('utf8',$link); 
// $link is optional, refers to your DB connection

回车以输出,请将输出文本包装在 nl2br ($ output)

If you wish breaks/carriage returns to be output, wrap your output text in nl2br($output)

这篇关于从数据库表打印文本时的字符集问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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