Dec
27
2010

Wrap Text in PHP

You can make text wrap in a browser in various ways, including using tables, iframes, and textareas, but sometimes you need absolute control over the wrapping in terms of the number of characters at which the wrap should occur, regardless of whether the user resizes their browser window.

Using this plug-in, it’s easy to pass a string of text and have it wrapped using <br />
tags. What’s more it can also indent the start of each new paragraph by an amount of your choosing.

This plug-in takes a string variable containing any text and adds <br /> and &nbsp; tags in
the right places to make the text wrap and indent paragraphs. It takes these arguments:

$text A string variable containing the text to be wrapped

$width An integer representing the character at which to force word wrapping
• $indent An integer representing the number of characters by which to indent each paragraph start

  • $wrapped String variable containing the wrapped text to be returned
  • $paragraphs Array containing the separate paragraphs as determined by n characters
  • $paragraph String containing an individual paragraph being processed
  • $words Array of all words in a paragraph
  • $word String containing the current word being processed
  • $len Numeric variable containing the length of the current line
  • $wlen Numeric variable containing the length of the next word to be processed.

The plug-in works by first splitting the text passed to it into separate paragraphs using the PHP explode() function with an argument of n, which is the newline character. What this function does is return an array of substrings based on splitting the original string each time a n is encountered. The function returns these paragraphs in the array $paragraphs.
A foreach loop is then entered passing $paragraphs as the input, and then each iteration of the loop places one paragraph at a time into the string variable $paragraph.

To transform unwrapped text into wrapped, call the function like this:
echo PIPHP_WrapText($message, 80, 5);
Here $message is the text to be wrapped, 80 is the character at which to force the wrapping, and 5 is the number of characters by which to indent the start of each paragraph.If you don’t want indenting, just set the third parameter to zero.

function PIPHP_WrapText($text, $width, $indent)
{
$wrapped = “”;
$paragraphs = explode(“n”, $text);
foreach($paragraphs as $paragraph)
{
if ($indent > 0) $wrapped .= str_repeat(“&nbsp;”, $indent);
$words = explode(” “, $paragraph);
$len = $indent;
foreach($words as $word)
{
$wlen = strlen($word);
if (($len + $wlen) < $width)
{
$wrapped .= “$word “;
$len += $wlen + 1;
}
else
{
$wrapped = rtrim($wrapped);
$wrapped .= “<br />n$word “;
$len = $wlen;
}
}
$wrapped = rtrim($wrapped);
$wrapped .= “<br />n”;
}
return $wrapped;
}

Enter Your Mail Address

Incoming search terms:

Related And Similar Posts

Leave a comment

*

Facebook Fans

Share Me With Google Plus!

Popular Posts

Social Bookmarking sites list 2012 - New and fresh bookmarking sites
Comments93
Bulgarian proxy or how to open arenabg and zamunda
Comments65
+ 150 New Free Directory Sites List 2012 - Sorted by Page Rank
Comments22
Scrapebox crack free download ?
Comments19
Great List With Google Plus / FB likes/ tweets exchange sites - Hot Topic
Comments16
How to make World Of Warcraft Cataclysm Server
Comments10
The Best Bulgarian Torrents - Listing
Comments9
500 Free Directory List 2011 !
Comments9
Free RSS Directory List Where To Submit Your Site
Comments9
Guest Blogs List and Useful Information
Comments8

Recent Posts

Calendar

December 2010
M T W T F S S
    Jan »
 12345
6789101112
13141516171819
20212223242526
2728293031