public function wordWrapText($text, $wrapWidth)
    {
        $wrappedText = '';
        foreach (explode(PHP_EOL, $text) as $line) {
            $words = explode(' ', $line);
            $currentLine = array_shift($words);
            while (count($words) > 0) {
                $word = array_shift($words);
                if ($this->getTextWidth($currentLine . ' ' . $word) > $wrapWidth) {
                    $wrappedText .= PHP_EOL . $currentLine;
                    $currentLine = $word;
                } else {
                    $currentLine .= ' ' . $word;
                }
            }
            $wrappedText .= PHP_EOL . $currentLine;
        }
        return ltrim($wrappedText, PHP_EOL);
    }
public function wordWrapText($text, $wrapWidth)
    {
        $wrappedText = '';
        foreach (explode(PHP_EOL, $text) as $line) {
            $words = explode(' ', $line);
            $currentLine = array_shift($words);
            while (count($words) > 0) {
                $word = array_shift($words);
                if ($this->getTextWidth($currentLine . ' ' . $word) > $wrapWidth) {
                    $wrappedText .= PHP_EOL . $currentLine;
                    $currentLine = $word;
                } else {
                    $currentLine .= ' ' . $word;
                }
            }
            $wrappedText .= PHP_EOL . $currentLine;
        }
        return ltrim($wrappedText, PHP_EOL);
    }
public function wordWrapText ( $ text , $ wrapWidth ) { $ wrappedText = '' ; foreach ( explode ( PHP_EOL , $ text ) as $ line ) { $ words = explode ( ' ' , $ line ) ; $ currentLine = array_shift ( $ words ) ; while ( count ( $ words ) > 0 ) { $ word = array_shift ( $ words ) ; if ( $ this -> getTextWidth ( $ currentLine . ' ' . $ word ) > $ wrapWidth ) { $ wrappedText .= PHP_EOL . $ currentLine ; $ currentLine = $ word ; } else { $ currentLine .= ' ' . $ word ; } } $ wrappedText .= PHP_EOL . $ currentLine ; } return ltrim ( $ wrappedText , PHP_EOL ) ; }
Word-wrap a text to a certain width, using the current font properties

@param string $text text to wrap (can already contain some newlines)
@param string $wrapWidth width (in the given units) to wrap the text to
@return string the same text but with newlines inserted at the specified $wrapWidth
Word - wrap a text to a certain width using the current font properties