PHP paginate string problem
i have this script
$content = string
if(!isset($_GET['page'])){
$page = 1;
}
else{
$page = $_GET['page'];
}
while($content[$limit]!= ' '){
$limit++;
}
print substr($content,($page-1)*$limit, $limit);
it works just fine first time (if the page is not set or page number is 1 )but 开发者_如何学Pythonif my page number is greater than 1 it splits my words. cause $limit is increased and even $limit is set to an offset that corresponds to a space character the start in the substr() function is increased too and it basically shifts the whole chunk and the last character that i get is not my wanted space but first couple of letters after the space.(i hope i made myself understood). how can i fix this problem? thanks
Try this:
print substr($content, ($page-1) * $limit, strpos ( $content, ' ' , ($page-1) * $limit));
This says:
Take the sub-string of $content, starting at the given page * the limit for each page, and given me all the characters up to the position of the next space.
As Frank Farmer said, use wordwrap, but with a weird delimiter. Then use explode with that delimiter to get an array.
$limit = 500; // max chars per page?
$content = "lots of text ..."; // the whole article?
if(!isset($_GET['page'])){
$page = 1;
} else {
$page = intval($_GET['page']);
}
$pages = explode("@BREAK@", wordwrap($content, $limit, "@BREAK@"));
print $pages[$page-1];
精彩评论