$text = addslashes($text); $wrap_at = 85; $text = preg_replace('%(\s*)([^>]{'.$wrap_at.',})(<|$)%e', "'\\1'.wordwrap('\\2', '".$wrap_at."', ' ', 1).'\\3'", $text); $text = stripslashes($text);