Forcing a break on big words / multiple characters

Visiting Digg today I notice this:
big words - digg example

This is really common problem on user generated sites. HTML does not force a break when the word or multiple characters are longer then the provided space.

I normally solve this in PHP with something like this:

function mywrap($string, $max_length)
{
$separate_words = explode(" ", $string);
for($i = 0; $i < count($separate_words); $i++) { if( strlen($separate_words[$i]) > $max_length )
{
$separate_words[$i] = wordwrap($separate_words[$i], $max_length,"<br />", true);
}
}
$string = implode(" ", $separate_words);
return $string;
}

Where $string is the text to verify and $max_length is the max number of characters in the space provided.

Hope it helps someone

Leave a Reply

Your email address will not be published. Required fields are marked *