This code works nicely:
<?
$usercomments = "Hello there how are you? This is a looooooooooooooooooooooooooooooooooooooong output";
$maxlength = 20;
$array = split(" ", $usercomments);
$c = count($array);
for ($i=0;$i<$c;$i++) {
if (strlen($array[$i]) > $maxlength) {
$output .= substr($array[$i], 0, $maxlength) . " ";
}
else {
$output .= $array[$i] . " ";
}
}
echo $output;
?>
replace $usercomments with the variable that is being passed from a form. $maxlength is the maximum length that you want to allow a word to be. Basically, this script will cut off a long word to how ever many characters you specify in $maxlength
Happy PHPing 🙂