Jump to content

[SOLVED] Breaking apart a long text article


londonjustin

Recommended Posts

Hi all,

 

I thought I'd throw this out there in case anyone had any ideas. I'm trying to find a way of breaking up a long article of text (a magazine feature for instance) so I can split it into pages.

 

There's obviously plenty of code out there for paginating resultsets but I've yet to find anything usable for paginating a feature. My theory at present is as follows:

 

1 - get the feature from the database

2 - search for the nearest line break between 500 and 600 words (or so) - use strpos to mark that position

3 - insert some sort of code (a div close) at that point, then repeat from the strpos marker until the end of the article

 

I have a javascript tabbed content script which has proven quite handy (http://www.dynamicdrive.com/dynamicindex17/tabcontent.htm) and I think I can adapt that into page 1, page 2 tabs.

 

I'm going to try this idea out shortly, but I'd be keen to hear if anyone has any ideas or experience on this - I've scoured the web and not found anything I can use so far.

 

thanks

J

what about explode to break them up. then use foreach on the array

 

explode

 

ie (this is by spaces  not CR but you get the idea)

<?php
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
foreach($pieces as $p)
{
echo "<div name='Blar'>";
echo $p;
echo "</div>";
}
?>

Thanks madTechie - that looks like a pretty nice and simple solution.

 

I guess the issue I have to work out is how to break the article into chunks of a similar size (e.g. 500 words) and then make sure the break occurs at a line break, rather than mid sentence or mid paragraph. Something like this maybe?

 

$startposition = 0;
$pizza = 'a very long article... etc';
$articlechunk= substr($pizza, $startposition, 550);
    $breakpoint = strrpos($articlechunk, "'/ln");
    $articlechunk= substr($articlechunk, 0, $breakpoint );
$startposition = $breakpoint; // send back to loop

whats wrong with using explode("\n", $article) ?

 

am i right in thinking that will break up the article into individual paragraphs? if so i could probably use that and have, say, 10 paragraphs on each page.

 

How would I then get 10 para on the first page - the next 10 on page 2, etc - use something like $paracount++ within the loop?

 

Thanks again - i appreciate this.

This will take a text file and break it into N paragraphs per page

 

<?php 
$text = file_get_contents('myfile.txt');

$paras = explode("\r\n\r\n", $text);

$pages = array();
$parasPerPage = 10;
$k = count($paras);

for ($p=0; $p<$k; $p+=$parasPerPage) {
    $pages[] = '<p>' . join ('</p><p>', array_slice($paras, $p, $parasPerPage)) . '</p>';
}
/**
* output the pages, with rule between each
*/

foreach ($pages as $pagetext) {
    echo $pagetext, '<hr />';
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.