Limit length of a word/string (FIXED)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
synical21
Forum Contributor
Posts: 150
Joined: Tue Jul 28, 2009 8:44 am
Location: London UK

Limit length of a word/string (FIXED)

Post by synical21 »

Hey Guru's,

I have a small problem, if a user submits data which is 1 word but 600 charcters long it will run off my template and make the page width really wrong when that word is output. I would like to limit the size of words so they do not run outside my page. I have done some googleing and came across php's wordwrap so I tried to implement it.

Code: Select all

 
$theproof = nl2br($row['proof']);
echo wordwrap($theproof, 20, "<br />\n");
 
Have I done it wrong? Is there a better way? Just a little help please :drunk:
Last edited by synical21 on Thu Jan 21, 2010 5:04 pm, edited 1 time in total.
synical21
Forum Contributor
Posts: 150
Joined: Tue Jul 28, 2009 8:44 am
Location: London UK

Re: Limit length of a word/string (FIXED)

Post by synical21 »

My bad I fixed it now, I forgot to use the parameter cut = TRUE E.G

Code: Select all

 
$theproof = nl2br($row['proof']);
echo wordwrap($theproof, 20, "<br />\n,",true);
 
If the cut is set to TRUE, the string is always wrapped at or before the specified width. So if you have a word that is larger than the given width, it is broken apart.
Post Reply