?
Solved

Paragraph Text Split

Posted on 2006-04-06
5
Medium Priority
?
1,158 Views
Last Modified: 2010-05-18
Ok so here it is... the behemoth of a question : )

1. I have a string which contains an unspecified amount of text... say 6 paragraphs...
2. I'm going to be displaying this paragraph in a table in a popup window (the table could be any height)
3. The text will not be in a scrollable window... it will be displayed directly on the page.
4. If the text is to long for the current page, then there should be a next page
5. If there is another page - next page, if previous page - previous page
6. If one paragraph finishes and another paragraph is still to be displayed but that next paragraph cannot display a min of x lines within the page size then the next paragraph should be displayed on the next page
7. A paragraph should only display on the page if it is able to fit at least x number of lines on the page, or if it is the first paragraph on the page in which case it would display as much of the paragraph it could and then complete it on the next page.
8. The last line of the page should always complete the sentence. IE not splitting the pages in the middle of sentences.

Any one up for the challenge... this should be a class or function...
0
Comment
Question by:phenixfilms
  • 2
3 Comments
 
LVL 10

Expert Comment

by:wildzero
ID: 16397411
Do you have some code showing what you have tried?
0
 
LVL 6

Expert Comment

by:WilliamFrantz
ID: 16405433
<?php
$width = 15;
$height = 3;
$text =
'Mary had a little lamb, whose fleece was white as snow.
Everywhere that Mary went, the lamb was sure to go.';

$document = array();
foreach (explode("\n", $text) as $paragraph)
{
    $document[] = explode("\n", wordwrap($paragraph, $width, "\n", true));
}
$page = array();
$pages = array();
foreach ($document as $paragraph)
{
    foreach ($paragraph as $i => $line)
    {
        $page[] = $line;
        if (count($page) == $height)
        {
            $pages[] = $page;
            $page = array();
        }
    }
}
if (count($page)) $pages[] = $page;

echo '<pre>';
print_r($pages);
?>

Output:

Array
(
    [0] => Array
        (
            [0] => Mary had a
            [1] => little lamb,
            [2] => whose fleece
        )

    [1] => Array
        (
            [0] => was white as
            [1] => snow.
            [2] => Everywhere that
        )

    [2] => Array
        (
            [0] => Mary went, the
            [1] => lamb was sure
            [2] => to go.
        )

)

So that's your array of pages.  This doesn't do the widow/orphan control you describe in #6 and #7.  Rule #8 will be difficult to meet unless you better define what a complete sentence is.  You can't just look for periods because of abbreviations and such.
0
 
LVL 6

Accepted Solution

by:
WilliamFrantz earned 1520 total points
ID: 16406139
Small enhancement to handle widows...

$widow = 2;
$document = array();
foreach (explode("\n", $text) as $paragraph)
{
    $document[] = explode("\n", wordwrap($paragraph, $width, "\n", true));
}
$page = array();
$pages = array();
foreach ($document as $paragraph)
{
    /* if there are less than 2 blank lines on this page and there are more
     * than 2 lines in this paragraph, move to the next page
     */
    if ($height - count($page) < $widow && count($paragraph) > $widow)
    {
        $pages[] = $page;
        $page = array();
    }
    foreach ($paragraph as $i => $line)
    {
        $page[] = $line;
        if (count($page) == $height)
        {
            $pages[] = $page;
            $page = array();
        }
    }
}
if (count($page)) $pages[] = $page;
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question