Page 1 of 1

Drop Text Down a Line after so many characters

Posted: Fri Sep 07, 2007 1:14 pm
by jasonfarrell
I need to set a maximum line length of a string. For example if the string is "Testing Test Tester Test" and I want only 5 characters per string, it needs to knock the next line down. I also need it to keep words together if it's under the 5 characters possible. So it would be:
"Testi
ng
Test
Teste
r
Test"

I'm using imagettftext() to create a box of text over a graphic, but there has to be a limit to how long each line of text is, and I just can't figure this out.

Posted: Fri Sep 07, 2007 2:24 pm
by s.dot
wordwrap() which gives you the option to choose your line cutter (defaults to \n, so you're good with the default) and the option to keep whole words.

also str_split() (php >= 5) and some nifty checks for whole words (checking for spaces)