存储和显示带有上标文本的文本

发布于 2024-12-15 23:45:46 字数 1406 浏览 1 评论 0原文

我将以下数据存储在 mysql 表的“call_dtls”字段中。此数据存储为 TEXT 数据类型。使用

在 mysql 中存储数据的代码:

$mycalltext = mysql_real_escape_string($_POST['text']);
// then storing $mycalltext in the table

存储在表中的数据(每行存储在单独的表行中):

SANDY505™ (09-11-11 10:04:47): buy hinduniliver around 385-383 sl 380 trgt 390-393
SANDY505™ (09-11-11 10:05:57): buy tatasteel around 472-468 sl 464 trgt 476-480
SANDY505™ (09-11-11 10:06:09): buy nifty around 5295-5280 sl 5260 trgt 5320

当另一个页面稍后获取并显示数据时, ™ 被 � 替换,所有内容都显示为单个行(换行符被忽略)。示例:

SANDY505� (09-11-11 10:04:47):在 385-383 sl 380 trgt 390-393 左右购买 hinuniliver SANDY505� (09-11-11 10:05:57):在 385-383 sl 左右购买 tatasteel 472-468 SL 464 TRGT 476-480 SANDY505� (09-11-11 10:06:09): 购买漂亮的 5295-5280 sl 5260 trgt 5320 

我希望输出为(包括换行符):

SANDY505™ (09-11-11 10:04:47): buy hinduniliver around 385-383 sl 380 trgt 390-393
SANDY505™ (09-11-11 10:05:57): buy tatasteel around 472-468 sl 464 trgt 476-480
SANDY505™ (09-11-11 10:06:09): buy nifty around 5295-5280 sl 5260 trgt 5320

产生输出的代码:

$rs=mysql_query("Select * from mya_calls", $cn) or die("MySQL error: ".mysql_errno());
$number=mysql_num_rows($rs); 
while ($rsitem=mysql_fetch_object($rs)) 
    echo $rsitem->call_dtls;

I have the following data stored in a field "call_dtls" in a mysql table. This data is stored as TEXT datatype. Data is submitted using a <textarea> control from a PHP-generated page.

Code for storing data in mysql:

$mycalltext = mysql_real_escape_string($_POST['text']);
// then storing $mycalltext in the table

The Data which gets stored in the table (each line is stored in a separate table row):

SANDY505™ (09-11-11 10:04:47): buy hinduniliver around 385-383 sl 380 trgt 390-393
SANDY505™ (09-11-11 10:05:57): buy tatasteel around 472-468 sl 464 trgt 476-480
SANDY505™ (09-11-11 10:06:09): buy nifty around 5295-5280 sl 5260 trgt 5320

When another page later fetches and displays the data, the ™ gets replaced by � and everything is displayed as a single line (the line breaks are ignored). Example:

SANDY505� (09-11-11 10:04:47): buy hinduniliver around 385-383 sl 380 trgt 390-393 SANDY505� (09-11-11 10:05:57): buy tatasteel around 472-468 sl 464 trgt 476-480 SANDY505� (09-11-11 10:06:09): buy nifty around 5295-5280 sl 5260 trgt 5320 

I want the output to be (line breaks included):

SANDY505™ (09-11-11 10:04:47): buy hinduniliver around 385-383 sl 380 trgt 390-393
SANDY505™ (09-11-11 10:05:57): buy tatasteel around 472-468 sl 464 trgt 476-480
SANDY505™ (09-11-11 10:06:09): buy nifty around 5295-5280 sl 5260 trgt 5320

Code that produces output:

$rs=mysql_query("Select * from mya_calls", $cn) or die("MySQL error: ".mysql_errno());
$number=mysql_num_rows($rs); 
while ($rsitem=mysql_fetch_object($rs)) 
    echo $rsitem->call_dtls;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

狼亦尘 2024-12-22 23:45:47

仔细检查数据库连接编码和输出 HTML 的编码是否与数据库上的编码匹配(可能是 UTF-8)

DB Connection:

mysql_query('SET CHARACTER SET utf8, NAMES utf8');

Output headers:

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

HTML:

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

Doublecheck that the database connection encoding and the encoding of your outputted HTML match the encoding on your database (probably UTF-8)

DB Connection:

mysql_query('SET CHARACTER SET utf8, NAMES utf8');

Output headers:

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

HTML:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
你的往事 2024-12-22 23:45:47

明白了......:-)

php 函数 nl2br()为我做这个。解决了这两个问题......

从谷歌搜索中得到这个并在 此网站

感谢@outis 对错误消息的解释。

got it...... :-)

the php function nl2br() does this for me. solves both issues.....

got this from googling and found solution at this site

thanks @outis for explanation about error message.

夜巴黎 2024-12-22 23:45:47

一般来说,HTML 中的 空白 是折叠的。例外情况是

 元素以及 CSS white-space 属性不是“正常”属性(特别是,仅当 white-space 为“预”、“预包装”和“预行”)。

在多行上显示内容是一个演示问题;不要使用 HTML 来获得您想要的效果。相反,选择最合适的元素来定义数据的结构。例如,示例看起来是一个无序列表,在这种情况下

    就合适。

try {
    $result = $db->query("SELECT call_dtls FROM mya_calls");
    $result->setFetchMode(PDO::FETCH_CLASS, 'StdClass');
    ?>
    <ul>
      <?php foreach ($result as $item) { ?>
        <li><?php echo $item->call_dtls; ?></li>
      <?php } ?>
    </ul>
    <?php
} catch (PDOException $exc) {
    ...
}

完成此操作后,如有必要,请使用 CSS 设置元素的样式。

请注意,示例代码是如何在 HTML 输出中使用语义元素的示例,不一定适合生产代码。在开发过程中,不同的关注点应该分为不同的模块。

In general, white-space in HTML is collapsed. The exceptions are within <pre> elements and elements for which the CSS white-space property is something other than "normal" (in particular, line breaks are preserved only when white-space is "pre", "pre-wrap" and "pre-line").

Displaying content on multiple lines is a presentation issue; don't use HTML to get the affect you want. Instead, pick the most appropriate elements to define the structure of the data. For example, the sample looks to be an unordered list, in which case a <ul> would be appropriate.

try {
    $result = $db->query("SELECT call_dtls FROM mya_calls");
    $result->setFetchMode(PDO::FETCH_CLASS, 'StdClass');
    ?>
    <ul>
      <?php foreach ($result as $item) { ?>
        <li><?php echo $item->call_dtls; ?></li>
      <?php } ?>
    </ul>
    <?php
} catch (PDOException $exc) {
    ...
}

Once you've done that, use CSS to set the style for the elements, if necessary.

Note that the sample code is an example of how to use semantic elements in HTML output and not necessarily suitable for production code. In development, different concerns should be separated into different modules.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文