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 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(“ ”, $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;
}
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
93 |
Bulgarian proxy or how to open arenabg and zamunda
65 |
+ 150 New Free Directory Sites List 2012 - Sorted by Page Rank
22 |
Scrapebox crack free download ?
19 |
Great List With Google Plus / FB likes/ tweets exchange sites - Hot Topic
16 |
How to make World Of Warcraft Cataclysm Server
10 |
The Best Bulgarian Torrents - Listing
9 |
500 Free Directory List 2011 !
9 |
Free RSS Directory List Where To Submit Your Site
9 |
Guest Blogs List and Useful Information
8 |
Recent Comments
- punjab singh on New Social Bookmarking Sites List PR5-PR8 Only -2012
- Georgi on New Article Directory Sites List – May 2012 + 200 quality sites PR6-PR0
- Djesur on Bulgarian proxy or how to open arenabg and zamunda
- Djesur on Bulgarian proxy or how to open arenabg and zamunda
- Djesur on Bulgarian proxy or how to open arenabg and zamunda

By admin
359 views






