note 110904 modified in function.str-split by danbrown
| From: | danbrown@php.net | Date: | Thu, 17 Jan 2013 15:32:07 +0000 |
| Subject: | note 110904 modified in function.str-split by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-193156@lists.php.net to get a copy of this message | ||
An easy way to split long texts in columns/pages with entire last words
<?php
$text = "This is a very very long text ... the end"; // ie 7000 cars.
$max_car = 1500; // content length of each page/col
// we first split the text in equal parts
$pages = str_split($text, $max_car);
// we now get something like
// $pages 1 = "This is a .... very long te"
// $pages 2 = "xt but first/last words are cutted"
// we now find each last whitespace of each col/page
foreach ($pages as $content) {
$content = $report.$content; //add the reported cars of previous page (ie "te" to get
"text" on page2)
$last_space = strrpos($content," ");
$page = substr($content,0,$last_space);
$report = str_replace($page,'',$content); // reported cars
// we now get
// $pages 1 = "This is a .... very long"
// $pages 2 = "text and first/last words are OK"
echo $page;
}
?>
--was--
An easy way to split long texts in columns/pages with entire last words
$text = "This is a very very long text ... the end"; // ie 7000 cars.
$max_car = 1500; // content length of each page/col
// we first split the text in equal parts
$pages = str_split($text, $max_car);
// we now get something like
// $pages 1 = "This is a .... very long te"
// $pages 2 = "xt but first/last words are cutted"
// we now find each last whitespace of each col/page
foreach ($pages as $content) {
$content = $report.$content; //add the reported cars of previous page (ie "te" to get
"text" on page2)
$last_space = strrpos($content," ");
$page = substr($content,0,$last_space);
$report = str_replace($page,'',$content); // reported cars
// we now get
// $pages 1 = "This is a .... very long"
// $pages 2 = "text and first/last words are OK"
echo $page;
}
http://php.net/manual/en/function.str-split.php