wordwrap()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sk8erh4x0r
Forum Commoner
Posts: 43
Joined: Wed May 26, 2004 8:27 pm

wordwrap()

Post by sk8erh4x0r »

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?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

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

Post by sk8erh4x0r »

no it doesn't. It won't split one string into 2.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

'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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

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;
}
Seems to work... ;)
Post Reply