You are here: Re: Need help breaking up text for multiple pages « PHP Programming Language « IT news, forums, messages
Re: Need help breaking up text for multiple pages

Posted by Jerry Stuckle on 04/27/06 17:12

m.shidoshi@gmail.com wrote:
> I was recently put in charge of heading up my company's website, and
> while I have a lot of experience on the design side of things, I'm
> still very new to the programming side. When I started, the website had
> just gotten a revision, but the site was an utter mess, so I've been
> trying to fix it up. As I've gone along, I've learned some aspects of
> PHP, but my knowledge is still very limited.
>
> Here is the problem I'm trying to fix. A backend is used to enter
> information into our MySQL database, and then a page template pulls it
> out to make the page. For our reviews, the PHP coding currently breaks
> up the review's text so that it can be places across multiple pages.
> The problem is, the way the text breaking was set up, the end of one
> page and the beginning of the next are totally chaotic, with breaks
> often coming mid-sentence.
>
> Here is what I'd love: other coding automatically puts an HTML break at
> the end of paragraphs, and then another in the space between one
> paragraph and the next. I'd love to have the code search for when those
> double breaks come up, and then break up the text after, say, every
> sixth pair of HTML breaks. I'd also like the option to break the text
> myself by putting in some sort of text code in the original text. At
> this point, though, anything that can make the breaks more elegant
> would be a huge improvement.
>
> I really appreciate any help anybody can give me in this one. Please
> remember that I know very little of PHP, and I'm not much of a
> programmer anyhow, so things that you may take for granted that people
> would understand about PHP, I might not know.
>
> I tried to pull out what I thought was the current coding for the text
> break. Here it is. I'd also like to get rid of the part1/part2 factor.
> Before, they had the text breaking to help in fitting into the layout
> around an image, but I've fixed that so I no longer need the break. So,
> that portion no longer needs to be a factor.
>
>
> if ($page == ""){
> $page = 1;
> }
> $no_letters = strlen($Game_Full_Story);
> $times = ($no_letters / 3800) +1;
> $Game_Full_Story = wordwrap($Game_Full_Story,3800,"*#^");
> $Game_Full_Story = explode ("*#^", $Game_Full_Story);
> $pages = array();
> for ($i=0; $i<$times ; $i++){
> $part[$i] = $Game_Full_Story[$i];
> if ($i != 0){
> array_push($pages, $i);
> }
> }
> $page = $page-1;
> $story = $part[$page];
> if (strlen($story) < 460){
> $parts1 = $story;
> }else{
> $story = wordwrap($story,460,"*#^");
> $story = explode ("*#^", $story);
> $parts1 = $story[0];
> $count = count($story);
> $parts2="";
> for ($i=1; $i<$count; $i++){
> $parts2 .=$story[$i];
> $parts2 .=" ";
> }
> }
>

Well, could it be as simple as using strpos() to find the <br>s? (Actually in
this case stripos() would be better because it's not case sensitive) (Warning -
NOT tested!)

if (!isset($page) || $page == "")
$page = 1;

$pos = 0;
for ($cpage = 0; $cpage < $page; $cpage++) {
$pstart = $pos;
for ($cpar = 0; $cpar < 6; $cpar++) { // Change 6 to # of paras/page
$oldpos = $pos;
$pos = stripos($text, '<br>', $pos);
if ($pos === false) // no more found
break;
else
$pos++; // Point to next char and continue
}
}
if ($oldpos > $pstart)
echo substr($text, $pstart, $oldpos);
else
echo "End of article";

Probably not perfect, but it should get you started.

Alternatively, you could split the paragraphs into an array and just count the
number of items you need, i.e. (Again no tested)

if (!isset($page) || $page == "")
$page = 1;
$tarray = preg_split('/<br>/i', $text);
$pcount = 6; // Number of paragraphs per page
$startpar = ($page-1) * $pcount;
$endpar = min($startpar + $pcount, count($tarray)); // Don't go past end
for ($i = $startpar; $i < $endpar; $i+)
echo $tarray[$i];

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация