1

我想从 TEXT 列中回显 mysql 数据,并保留中断。我曾经<pre>这样做过,但这会忽略style="word-break:break-word;"

伪示例:

<?php echo '<pre>'; ?>
<div style="width: 100px; word-break:break-word">
LonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglong

LonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglong
</div>
<?php echo '</pre>'; ?>

(div里面的文字是从mysql里拉出来的)

break-word;在此示例中将不起作用,因为文本是用<pre>标签包裹的。如何保留原始文本的换行符,但在显示时诱导分词?

4

3 回答 3

3

使用nl2br功能。<br/>它会在需要新行时添加标签。

<div style="width: 100px; word-break:break-word">
    <?php echo nl2br($yourVeryLongTextWithLineBreaks); ?>
</div>
于 2012-10-13T10:47:47.507 回答
0

您可以使用wordwrap将线喙插入长字符串,特别是如果它们没有像您的示例那样的空间

$longText = 'LonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglong';
echo wordwrap($longText, 8, "<br />\n",true);
于 2012-10-13T10:48:14.127 回答
0

尝试这个;

<?php echo '<pre style="width:100px; word-wrap:break-word;">'; ?>
//some code
<?php echo '</pre>'; ?>
于 2012-10-13T10:48:26.477 回答