View Full Version : Word Wrap

08-24-2007, 04:39 AM
I have a simply comment system. I gave it a test with the regular idiotic aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa comment that we see every now and then, and as usual it messes up the layout, so I need some type of break in there. I decided to use the wordwrap, because last time I checked it worked. Not in my case though :(. I want a space after each 20th character. Why isn't this working?

$komenti=wordwrap($komenti, 20, " ");

08-24-2007, 04:46 AM
wordwrap() doesn't wrap long words, it just splits sentences up just like the HTML should do for you.

An example of word wrap:

$text = "The quick brown fox jumped over the lazy dog.";
$newtext = wordwrap($text, 20, "<br />\n");

echo $newtext;

The quick brown fox<br />
jumped over the lazy<br />

Try this:

$komenti=preg_replace('#\s(.{10,})(.*?)\s#', '$1 $2', $komenti);

08-24-2007, 04:54 AM
Actually wordwrap() will work fine for this, you just have to specify the fourth parameter as "true".

string wordwrap ( string $str [, int $width [, string $break [, bool $cut]]] )

If the cut is set to TRUE, the string is always wrapped at the specified width. So if you have a word that is larger than the given width, it is broken apart. (See second example).

08-24-2007, 05:08 AM
Worked likea charm. Thanks Fumigator.