存储和显示带有上标文本的文本
我将以下数据存储在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仔细检查数据库连接编码和输出 HTML 的编码是否与数据库上的编码匹配(可能是 UTF-8)
DB Connection:
Output headers:
HTML:
Doublecheck that the database connection encoding and the encoding of your outputted HTML match the encoding on your database (probably UTF-8)
DB Connection:
Output headers:
HTML:
明白了......:-)
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.
一般来说,HTML 中的 空白 是折叠的。例外情况是
在多行上显示内容是一个演示问题;不要使用 HTML 来获得您想要的效果。相反,选择最合适的元素来定义数据的结构。例如,示例看起来是一个无序列表,在这种情况下
就合适。
完成此操作后,如有必要,请使用 CSS 设置元素的样式。
请注意,示例代码是如何在 HTML 输出中使用语义元素的示例,不一定适合生产代码。在开发过程中,不同的关注点应该分为不同的模块。
In general, white-space in HTML is collapsed. The exceptions are within
<pre>
elements and elements for which the CSSwhite-space
property is something other than "normal" (in particular, line breaks are preserved only whenwhite-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.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.