Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 you.

like image 689
Francisc Avatar asked Dec 16 '22 19:12

Francisc


2 Answers

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");
like image 163
Breezer Avatar answered Jan 06 '23 07:01

Breezer


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

like image 31
Shakeeb Ahmed Avatar answered Jan 06 '23 07:01

Shakeeb Ahmed