Is there a way to wrap text even when it's one long string?
ex:
thisisastringoftextthatneedstobewordwrapped
Can wordwrap() be used to split that up or would i need to use something else?
wordwrap()
Moderator: General Moderators
wordwrap() will split the string where you tell it to, so it will wrap that.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
-
sk8erh4x0r
- Forum Commoner
- Posts: 43
- Joined: Wed May 26, 2004 8:27 pm
- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact:
'wordwrap()' wraps the string at a position specified and introduces a newline character and puts the remaining string behind it which will allow to display properly in HTML.
if property cut is set, the string is terminated at the 'x' position you specify.
I think this might be useful when you want to provide headlines of some XML news feeds.
alright, lets assume a string is of 200 characters, you specify the wrap position at 70, so it puts a '<br />' at 70th position but the remaining string is longer than 70chars. wot it will do now???
Oh...I did not look at the other example, it wraps at every 70th position....good function...badly needed for overflowing div content.
if property cut is set, the string is terminated at the 'x' position you specify.
I think this might be useful when you want to provide headlines of some XML news feeds.
alright, lets assume a string is of 200 characters, you specify the wrap position at 70, so it puts a '<br />' at 70th position but the remaining string is longer than 70chars. wot it will do now???
Oh...I did not look at the other example, it wraps at every 70th position....good function...badly needed for overflowing div content.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Code: Select all
/*
Wraps text at the given length of characters without splitting words
-- in bits (unless it really needs to).
*/
function wrap_text($text, $length, $end='<br />')
{
$tokens = preg_split('/\b/', $text); //Tokenize the string (maybe strtok() is better for this?)
$temp = array();
$out = '';
$tmp = '';
foreach ($tokens as $tok)
{
if (strlen($tmp.$tok) <= $length)
{
$tmp .= $tok;
}
elseif (strlen($tok) > $length) //The single word is too long => split it!
{
$out .= !empty($tmp) ? $tmp.$end : ''; //Build onto string
$tmp = ''; //Reset tmp
do
{
if (strlen($tok) > $length) $y = false;
else $y = true;
$out .= substr($tok, 0, $length).$end; //Build onto string
$tok = substr($tok, $length); //Move along the token string
} while (!$y); //End do..while
}
else
{
$out .= $tmp.$end; //Build onto string
$tmp = ''; //Reset tmp
} //End if
} //End foreach
return $out;
}