...

View Full Version : TD word wrap



Ludatha
11-06-2009, 05:51 PM
I cannot get the word wrap to work on my site, the text is inside a TD, can anyone help?

Examples:
http://ludatha.com/forum/category/Spam%20Land/

http://ludatha.com/forum/post/628-WordWrap/

Long sentences and long words don't wrap to the next line.


th {background-color:#888; height:30px; color:#111; border-bottom:#666 1px solid; word-wrap:break-word;}

td {padding:5px; background-color:#999; border-bottom:#666 1px solid; word-wrap:break-word;}

table {width:97%; word-wrap:break-word;}

oesxyl
11-06-2009, 06:02 PM
I cannot get the word wrap to work on my site, the text is inside a TD, can anyone help?

Examples:
http://ludatha.com/forum/category/Spam%20Land/

http://ludatha.com/forum/post/628-WordWrap/

Long sentences and long words don't wrap to the next line.


th {background-color:#888; height:30px; color:#111; border-bottom:#666 1px solid; word-wrap:break-word;}

td {padding:5px; background-color:#999; border-bottom:#666 1px solid; word-wrap:break-word;}

table {width:97%; word-wrap:break-word;}
white-space: normal is used to wrap but don't work in your example because is a single long word and wrap is applied to words, can't split and wrap a word.
You must use some server side scripting to split or cut the word and/or you can use client side, javascript for example, to disallow posting such thing at validation time. Be aware that javascript can be manipulated and therefor you must also use a server side validation rutine for that.

best regards

VIPStephan
11-06-2009, 06:15 PM
http://www.brunildo.org/test/IEbreak-word.html

Ludatha
11-06-2009, 08:49 PM
white-space: normal is used to wrap but don't work in your example because is a single long word and wrap is applied to words, can't split and wrap a word.
You must use some server side scripting to split or cut the word and/or you can use client side, javascript for example, to disallow posting such thing at validation time. Be aware that javascript can be manipulated and therefor you must also use a server side validation rutine for that.

best regards

Thanks, I wanted to know if there was an easy CSS cheat :P

So I went for the PHP option, and here is my code if anyone finds this thread with the same problem (from php.net but some modifications)


function wordWrapIgnoreHTML($string, $length = 45, $wrapString = "\n"){
$wrapped = '';
$word = '';
$html = false;
$string = (string) $string;
for($i=0;$i<strlen($string);$i+=1)
{
$char = $string[$i];

/** HTML Begins */
if($char === '<')
{
if(!empty($word))
{
$wrapped .= $word;
$word = '';
}

$html = true;
$wrapped .= $char;
}

/** HTML ends */
elseif($char === '>')
{
$html = false;
$wrapped .= $char;
}

/** If this is inside HTML -> append to the wrapped string */
elseif($html)
{
$wrapped .= $char;
}

/** Whitespace characted / new line */
elseif($char === ' ' || $char === "\t" || $char === "\n" || $char === "0")
{
$wrapped .= $word.$char;
$word = '';
}

/** Check chars */
else
{
$word .= $char;

if(strlen($word) > $length)
{
$wrapped .= $word.$wrapString;
$word = '';
}
}
}

if($word !== ''){
$wrapped .= $word;
}

return $wrapped;
}

oesxyl
11-06-2009, 09:08 PM
Thanks, I wanted to know if there was an easy CSS cheat :P

So I went for the PHP option, and here is my code if anyone finds this thread with the same problem (from php.net but some modifications)


function wordWrapIgnoreHTML($string, $length = 45, $wrapString = "\n"){
$wrapped = '';
$word = '';
$html = false;
$string = (string) $string;
for($i=0;$i<strlen($string);$i+=1)
{
$char = $string[$i];

/** HTML Begins */
if($char === '<')
{
if(!empty($word))
{
$wrapped .= $word;
$word = '';
}

$html = true;
$wrapped .= $char;
}

/** HTML ends */
elseif($char === '>')
{
$html = false;
$wrapped .= $char;
}

/** If this is inside HTML -> append to the wrapped string */
elseif($html)
{
$wrapped .= $char;
}

/** Whitespace characted / new line */
elseif($char === ' ' || $char === "\t" || $char === "\n" || $char === "0")
{
$wrapped .= $word.$char;
$word = '';
}

/** Check chars */
else
{
$word .= $char;

if(strlen($word) > $length)
{
$wrapped .= $word.$wrapString;
$word = '';
}
}
}

if($word !== ''){
$wrapped .= $word;
}

return $wrapped;
}
if it work as you expect is perfect, :) A note, is better to not mix code for two things in the same function, in this case removing html and word wrap, the probability to need one of them in another place is bigger then to need both, so you can reuse the code in other place on the site.

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum