Important alert: (current site time 7/15/2013 10:59:42 AM EDT)
 

VB icon

Better Word Wrap

Email
Submitted on: 8/5/2009 11:09:56 AM
By: Matt DeKok  
Level: Beginner
User Rating: Unrated
Compatibility: PHP 3.0, PHP 4.0, PHP 5.0
Views: 4000
author picture
(About the author)
 
     This can split a string into lines separated by a new line character of your choice. You can also choose whether you want it to split words for an exact length or not split words.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Better Word Wrap
// Description:This can split a string into lines separated by a new line character of your choice. You can also choose whether you want it to split words for an exact length or not split words.
// By: Matt DeKok
//
// Inputs:$text = Text to wrap
$line_length = (Default: 80)
$new_line_character = Default: <br />\n
$dont_split_words = Default: true
//
// Returns:A string with lines separated by the chosen new line character.
//
// Assumes:$text = "These lines won't necessarily be exactly the chosen length, so go ahead and trim the spaces at the beginning and end of the line.";
echo word_wrap($text, 20, '\n', false);
This will echo:
These lines won't ne
cessarily be exactly
 the chosen length, 
so go ahead and trim
 the spaces at the b
eginning and end of 
the line.
echo word_wrap($text, 20);
This will echo:
These lines won't<br />
necessarily be<br />
exactly the chosen<br />
length, so go ahead<br />
and trim the spaces<br />
at the beginning and<br />
end of the line.
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=2666&lngWId=8//for details.//**************************************

function word_wrap($text, $line_length = 80, $new_line_character = '<br />\n', $dont_split_words = true) {
// Holds the text lines
$lines = array();
// This checks if they want the lines to be the exact chosen length
// regardless if that means having to split words.
if($dont_split_words) {
// Separate the text by each word to ensure they won't be split
$words = explode(' ',$text);
// Set the current line to 0
$line = 0;
// Loop through the words one by one until the second to last word
for($i=0;$i<count($words)-1;$i++) {
// Add words one by one to the current line 
@$lines[$line] .= ' '.$words[$i];
// These lines won't necessarily be exactly the chosen length, so go
// ahead and trim the spaces at the beginning and end of the line.
$lines[$line] = trim($lines[$line]);
// If the next word will cause the line to exceed the chosen length,
// then increase the line count.
if(strlen($lines[$line]) + strlen($words[$i+1]) + 1 > $line_length) {
$line++;
}
}
// Add the last word
@$lines[$line] .= ' ' . $words[count($words)-1];
$lines[$line] = trim($lines[$line]);
} else {
// Retrieve the number of lines
$line_count = ceil(strlen($text) / $line_length);
// Loop through and pick out the part of the string to the chosen
// length and build each line
for($i=0;$i<$line_count-1;$i++) {
$lines[] = substr($text, $i * $line_length, $line_length);
}
// Make the last line whose length is unknown
$lines[] = substr($text,($line_count - 1) * $line_length);
}
// Join the lines by the specified new line character
return implode($new_line_character,$lines);
}


Other 3 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.