Hmmm yeh I ran into this problem in a recent project as well, this is the solution I came up with, it's not totaly pretty, but it works 🙂
//This is a nice little function, just copy and use to your hearts content
<?php
function fixstring($string, $MAX_LEN = 80)
{
$array = explode(" ", $string);
for($i=0;$i<count($array);$i++)
{
if(strlen($array[$i])>$MAX_LEN)
$array[$i] = chunk_split($array[$i], $MAX_LEN, " ");
}
return implode(" ", $array);
}
?>
//This is an example of how to use this code.
<?php
$string = "I like to addlotsofcharacterstogethertobeaproblematicchild but I am still friendly";
//Fix string, max 10 chars per word.
$newstring = fixstring($string, 10);
echo $string."<br>";
echo $newstring;
?>
//This is another example
<?php
$string = "I like to addlotsofcharacterstogethertobeaproblematicchild but I am still friendly";
//Fix string, using defalt value of 80.
$newstring = fixstring($string);
echo $string."<br>";
echo $newstring;
?>
This methood may not be the prettiest, but it works and it's easy to read 🙂