One way to do it is check if the string is longer than a maximum length and contains no spaces. If so, echo out the substr() of the string and append some ellipses, otherwise echo out the entire string. Something like (untested):