Paragraph Text Split

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...
LVL 1
phenixfilmsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wildzeroCommented:
Do you have some code showing what you have tried?
0
WilliamFrantzCommented:
<?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
WilliamFrantzCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.