回应'<pre>&#39; to preserve breaks conflicts with break-word</pre>

时间:2012-10-13 10:44:57

标签: php mysql css

我想从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>个标记。 如何保留原始文本的换行符,但在显示时会导致断字?

3 个答案:

答案 0 :(得分:3)

使用nl2br功能。当新行出现时,它会添加<br/>个标签。

<div style="width: 100px; word-break:break-word">
    <?php echo nl2br($yourVeryLongTextWithLineBreaks); ?>
</div>

答案 1 :(得分:0)

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

$longText = 'LonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglongLonglonglong';
echo wordwrap($longText, 8, "<br />\n",true);

答案 2 :(得分:0)

试试这个;

<?php echo '<pre style="width:100px; word-wrap:break-word;">'; ?>
//some code
<?php echo '</pre>'; ?>