Word Wrap


Normally when you have a site people tend to be rude and sign up with really long usernames or post one really long line of text on forums. Sure, there are other ways of fixing this like limiting the text box with:
PHP Code
  1. <input type="text" name="user" size="15" maxlength="20">

size is how wide it is and maxlength is how many characters the box will hold.

So, now on with the tutorial.

PHP Code
  1. <?php
  2. $string = "This is an example of a really really really long post that may stretch your page a few inches or pixels and make the layout look like crap"; //Demo string though you can replace it with something else
  3.  
  4. $wrap = wordwrap($string, 15, "\n"); //this tells the script to wrap the text to 15 characters per line.
  5.  
  6. echo "$wrap"; //Echo the wrapped Text
  7. ?>


That string there would come back as:
PHP Code
  1. This is an
  2. example of a
  3. really really
  4. really long
  5. post that may
  6. stretch your
  7. page a few
  8. inches or
  9. pixels and make
  10. the layout look
  11. like crap

That may look bad but in some situations it can make things better.
[ ] = Optional
PHP Code
  1. wordwrap ( string [, length [, break [, bool cut}}] );


If you don't put in a length the default is 75 i believe.
ShadowMage's Avatar
Author:
Views:
2,217
Rating:
There are currently no comments for this tutorial, login or register to leave one.