views:

60

answers:

2

I have some text which occasionally contains very long strings of characters, which is breaking my CSS.

This is an example:

http://iapps.smartphonesoft.com/php/software_details.php?id=358281487

whereas this one is fine

http://iapps.smartphonesoft.com/php/software_details.php?id=380894729

Is it possible to strip the very long line of *s in the above example down to a manageable length?

The extra complication is that is not always *s that cause the issue, ie in this example it is the = string which is causing the issue.

http://iapps.smartphonesoft.com/php/software_details.php?id=371255483

So I want to write PHP which carries out the function if a single 'word' within $description is >= 30 chars long, shrink it down to 30 chars.

All the text is kept within the variable $description

regards,

Greg

+1  A: 

you can explode string to array of words, then check strlen of every word not > 30

like

$words = explode(" ", $string);
foreach($words as $k=>$word){
   if(strlen($word) >= 30)
      $words[$k] = substr($word,0,30) ;
}

after that return array to one string with

join(' ' ,$words);
Haim Evgi
And concat with <br/> in the end of each line.
Topera
what is the length of the line ? after how many words ?
Haim Evgi
The problem with this is that, (at least with normal proportional fonts) different characters have different width. As line lengths aren't all that long, you can't rely on the average character width to even out the overall line length. This is why wrapping based on number of characters isn't all that good.
Delan Azabani
+3  A: 

By default, browsers won't wrap an extremely long word. You can override this behavior and always wrap, even if you need to break a word, with CSS (no PHP required!)

The property is word-wrap: break-word; and works in all browsers.

Delan Azabani
thanks, that is an excellent solution which has worked :)
kitenski
No problem, happy to help.
Delan Azabani