Vi antar att vi har en lång text i en sträng som innehåller meningar indelade i stycken (mha. tomma rader). Denna text vill ska pagineras så att t.ex. högst 10 rader per "sida". Vi vill endast ha hela stycken per "sida", om inte hela ryms så skall pagineringen ske innan stycket. Egentligen borde man väl behandla det på precis samma sätt som nöt #3:Veckans? nöt #5
Nedan en text som kan användas som test (hämtad från http://www.softwarereality.com/lifecycle/xp/extremers.jsp)
<info>
Refactor, yeah, yeah, yeah.
Refactor, yeah, yeah, yeah.
Refactor, yeah, yeah, yeah, yeah.
You say you wrote some code
And it smells pretty bad
Your pair programming buddy
Is lookin kinda sad
You just refactor
And then you're having fun
Refactor
Don't worry bout getting done
You've coded it ten times
But no one seems to mind
It passes unit tests
But its never been designed
So just refactor
Cause its all about havin fun
Refactor
Cause software's never done, oooooooooooooh!
Refactor, yeah, yeah, yeah.
Cause if your code smells clean your client won't get mad.
Doesn't matter when it's done
Or even what it does
Cause you're doin XP
And it's the latest buzz
So you refactor
Cause you know you will be glad
Refactor
And you never will be sad, oooooooooooooh!
Refactor, yeah, yeah, yeah.
Refactor, yeah, yeah, yeah.
Cause if your code smells clean your client won't get mad.
Cause if your code smells clean your client won't get mad.
Cause if your code smells clean your client won't get mad.
Yeah, yeah, yeah!
Yeah, yeah, yeah!
Yeah, yeah, yeah, yeah!
</info>Sv: Veckans? nöt #5
<style type="text/css">
pre {
border: 1px solid black;
background: #ffc;
padding: .5em;
}
</style>
<?php
$text = <<<END_OF_TEXT
Refactor, yeah, yeah, yeah.
Refactor, yeah, yeah, yeah.
Refactor, yeah, yeah, yeah, yeah.
You say you wrote some code
And it smells pretty bad
Your pair programming buddy
Is lookin kinda sad
You just refactor
And then you're having fun
Refactor
Don't worry bout getting done
You've coded it ten times
But no one seems to mind
It passes unit tests
But its never been designed
So just refactor
Cause its all about havin fun
Refactor
Cause software's never done, oooooooooooooh!
Refactor, yeah, yeah, yeah.
Cause if your code smells clean your client won't get mad.
Doesn't matter when it's done
Or even what it does
Cause you're doin XP
And it's the latest buzz
So you refactor
Cause you know you will be glad
Refactor
And you never will be sad, oooooooooooooh!
Refactor, yeah, yeah, yeah.
Refactor, yeah, yeah, yeah.
Cause if your code smells clean your client won't get mad.
Cause if your code smells clean your client won't get mad.
Cause if your code smells clean your client won't get mad.
Yeah, yeah, yeah!
Yeah, yeah, yeah!
Yeah, yeah, yeah, yeah!
END_OF_TEXT;
define('PAGE_LENGTH', 10);
define('LINE_BREAK', "\r\n"); // Olika för olika OS
function show_page($page)
{
echo '<pre>', implode(LINE_BREAK . LINE_BREAK, $page), '</pre>';
}
$paras = explode(LINE_BREAK . LINE_BREAK, $text);
$page = array();
$length = 0;
foreach($paras as $para) {
$para_length = count(explode(LINE_BREAK, $para));
if ($length + $para_length + 1 /* tom rad mellan stycken */ > PAGE_LENGTH) {
show_page($page);
$page = array();
$length = 0;
}
else {
$page[] = $para;
$length += $para_length;
}
}
if (count($page)>0)
show_page($page);
?>
Sv: Veckans? nöt #5
http://www.pellesoft.se/communicate/forum/view.aspx?msgid=157866&forumid=81&sum=0