开发者

PHP or HTML/CSS solution for forcing line breaks in table

开发者 https://www.devze.com 2023-01-28 02:58 出处:网络
I have a table filled with user generated text. The text spans for as much as the TD allows, however if the input is a string made of a long string with no breaking chars (white space, dash etc) it me

I have a table filled with user generated text. The text spans for as much as the TD allows, however if the input is a string made of a long string with no breaking chars (white space, dash etc) it messes up the table.

e.g. ggggggggggggggggggggggggggggggggggggggggggggggggggggg

How can I make it so that those strings wrap as well?

Thank开发者_JS百科 you.


You can use CSS:

table {
    table-layout: fixed;
    width: 100%;
}

table td, table th {
    word-wrap: break-word;       /* Internet Explorer 5.5+, Chrome, Firefox 3+ */
    overflow-wrap: break-word;   /* CSS3 standard: Chrome & Opera 2013+ */
}

/* Use this if you also want to preserve multiple spaces in text */
table td, table th {
    white-space: -moz-pre-wrap;  /* Firefox 1.0-2.0 */
    white-space: -pre-wrap;      /* Opera 4-6 */
    white-space: -o-pre-wrap;    /* Opera 7 */
    white-space: pre-wrap;       /* CSS3 */
}

Here is an example: http://www.jsfiddle.net/QPP8A/ (now out of date, sorry)

If you find this to hard to apply, you can use PHP's wordwrap function:

$text = "The quick brown fox jumped over the lazy dog.";
$newtext = wordwrap($text, 20, "<br />\n");


try this jquery solution it will break the text after number of letters defined:

http://plugins.jquery.com/project/Word-Break

You can use it like this.

$('yourtable td').wordwrap({
        width: 50,
        cut:true,
        brk: '<br/>\n'
        })

Thanks

0

精彩评论

暂无评论...
验证码 换一张
取 消