Wrap text every 2500 characters in a <div> for pagination using PHP or javascript

一曲冷凌霜 提交于 2019-12-01 09:34:37

Just add the </div> then?

$text = '<div class="individualPage">'
      . wordwrap($text, 5, '</div><div class="individualPage">')
      . '</div>';

However, you can do even better with javascript: you can paginate in response to the viewer's screen size.

Just set your HTML to:

<div id="target">...</div>

Add some css for pages:

#target {
    white-space: pre-wrap; /* respect line breaks */
}
.individualPage {
    border: 1px solid black;
    padding: 5px;    
}

And then use the following code:

var contentBox = $('#target');
//get the text as an array of word-like things
var words = contentBox.text().split(' ');

function paginate() {
    //create a div to build the pages in
    var newPage = $('<div class="individualPage" />');
    contentBox.empty().append(newPage);

    //start off with no page text
    var pageText = null;
    for(var i = 0; i < words.length; i++) {
        //add the next word to the pageText
        var betterPageText = pageText ? pageText + ' ' + words[i]
                                      : words[i];
        newPage.text(betterPageText);

        //Check if the page is too long
        if(newPage.height() > $(window).height()) {
            //revert the text
            newPage.text(pageText);

            //and insert a copy of the page at the start of the document
            newPage.clone().insertBefore(newPage);

            //start a new page
            pageText = null;
        } else {
            //this longer text still fits
            pageText = betterPageText;             
        }
    }    
}

$(window).resize(paginate).resize();

This will work in conjunction with the PHP solution, providing backwards compatibility if javascript is disabled.

This is the way I'd do it:

$out = '';
$text = str_split($text, 2500);
foreach($text as $t){
    $out .= "<div class='individualPage'>".$t."</div>";
}
echo $out;

EDIT: This will break apart words, so stick with wordwrap(). :D

Just add an opening div at the beginning, a closing div at the end, and closing divs at the beginning of every iteration.

$div = '<div class="individualPage">';
$text = $div . wordwrap($text, 5, "</div>$div") . '</div>'; 

In Javascript, there's not as good of a built in solution.

var s = text, div = "<div class='individualPage'>";
while(text.length > 5) {
  s = text.slice(0, 5) + "</div>" + div;
  text = text.slice(5);
}
s = div + s + "</div>";

Just for fun - here's a fairly ugly JavaScript RegExp that will paginate text and try to not break words. I'm not sure how well it would perform on a huge amount of text though.

var text = ....
// Grab 2500 (or slightly more if it doesn't exactly end on a word boundary)
// characters, or less than 2500 if it's at the end of the string.
text = text.replace(/(((.|\n){2500,2520}?\b)|((.|\n){1,2499}(?!.)))/mg, 
                    '<div class="individual-page">$1</div>')

jsFiddle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!