londonjustin Posted May 5, 2007 Share Posted May 5, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/50120-solved-breaking-apart-a-long-text-article/ Share on other sites More sharing options...
MadTechie Posted May 5, 2007 Share Posted May 5, 2007 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/50120-solved-breaking-apart-a-long-text-article/#findComment-246080 Share on other sites More sharing options...
londonjustin Posted May 5, 2007 Author Share Posted May 5, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/50120-solved-breaking-apart-a-long-text-article/#findComment-246085 Share on other sites More sharing options...
MadTechie Posted May 5, 2007 Share Posted May 5, 2007 whats wrong with using explode("\n", $article) ? Quote Link to comment https://forums.phpfreaks.com/topic/50120-solved-breaking-apart-a-long-text-article/#findComment-246091 Share on other sites More sharing options...
londonjustin Posted May 5, 2007 Author Share Posted May 5, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/50120-solved-breaking-apart-a-long-text-article/#findComment-246112 Share on other sites More sharing options...
MadTechie Posted May 5, 2007 Share Posted May 5, 2007 Humm, it all depends on the page height/width 10 para might not cut it, have a look at some of the User Contributed Notes on the [urlhttp://uk.php.net/wordwrap]wordwrap[/url] page Quote Link to comment https://forums.phpfreaks.com/topic/50120-solved-breaking-apart-a-long-text-article/#findComment-246166 Share on other sites More sharing options...
Barand Posted May 5, 2007 Share Posted May 5, 2007 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 />'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/50120-solved-breaking-apart-a-long-text-article/#findComment-246286 Share on other sites More sharing options...
londonjustin Posted May 5, 2007 Author Share Posted May 5, 2007 Genius! That worked a charm - thanks v much. Quote Link to comment https://forums.phpfreaks.com/topic/50120-solved-breaking-apart-a-long-text-article/#findComment-246355 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.