Как разбить текст на куски, учитывая предложения?

219
21 декабря 2019, 21:30

Всем привет. Необходимо, используя PHP 5.6, разбить текст, длина которого более 3000 символов на куски не более 500 символов. Но при этом нужно учитывать предложения. Текст может содержать теги. Например имеем текст, в нём 1398 символов:

Financial, commercial, legal, and intellectual factors changed the size of encyclopedias. During the <a href="/wiki/Renaissance" title="Renaissance">Renaissance</a>, middle classes had more time to read and encyclopedias helped them to learn more. Publishers wanted to increase their output so some countries like Germany started selling books missing alphabetical sections, to publish faster. Also, publishers could not afford all the resources by themselves, so multiple publishers would come together with their resources to create better encyclopedias. When publishing at the same rate became financially impossible, they turned to subscriptions and serial publications. This was risky for publishers because they had to find people that would pay all upfront or make payments. When this worked, capital would rise and there would be a steady income for encyclopedias. Later, rivalry grew, causing copyright to occur due to weak underdeveloped laws. Some publishers would copy another publisher's work to produce an encyclopedia faster and cheaper so consumers did not have to pay a lot and they would <a href="/wiki/encyclopedias" title="Renaissance">sell more</a>. Encyclopedias made it to where middle-class citizens could basically have a small library in their own house. Europeans were becoming more curious about their society around them causing them to revolt against their government.

Необходимо на выходе получить массив:

$result = [
   // 393 символа 
   0 => "Financial, commercial, legal, and intellectual factors changed the size of encyclopedias. During the <a href=\"/wiki/Renaissance\" title=\"Renaissance\">Renaissance</a>, middle classes had more time to read and encyclopedias helped them to learn more. Publishers wanted to increase their output so some countries like Germany started selling books missing alphabetical sections, to publish faster.",
    // 478 символов
    1 => "Also, publishers could not afford all the resources by themselves, so multiple publishers would come together with their resources to create better encyclopedias. When publishing at the same rate became financially impossible, they turned to subscriptions and serial publications. This was risky for publishers because they had to find people that would pay all upfront or make payments. When this worked, capital would rise and there would be a steady income for encyclopedias.",
    // 407 символов
    2 => "Later, rivalry grew, causing copyright to occur due to weak underdeveloped laws. Some publishers would copy another publisher's work to produce an encyclopedia faster and cheaper so consumers did not have to pay a lot and they would <a href=\"/wiki/encyclopedias\" title=\"Renaissance\">sell more</a>. Encyclopedias made it to where middle-class citizens could basically have a small library in their own house.",
    // Оставшаяся часть - 117 символов
    3 => "Europeans were becoming more curious about their society around them causing them to revolt against their government."
]; 

Подскажите как это лучше всего сделать.

Answer 1

Красоту сделаете сами...

$text = "Financial, commercial, legal..........against their government.";
$limit = 500;
$result = [];
$number = 0;
$array = array_map(function($v) { return trim($v) . '.'; }, array_filter(explode('.', $text)));
foreach ($array as $key => $value) {
    if ((mb_strlen($value) + mb_strlen($result[$number]) + 1) > 500) {
        $number++;
    }
    $result[$number] = (empty($result[$number])) ? $value : $result[$number] . ' ' . $value;
}
var_dump($result);
READ ALSO
Правильный запрос в MySQL через PHP

Правильный запрос в MySQL через PHP

Добрый деньВ данном запросе к базе данных, хочу реализовать поиск по ключу LIKE и поиску любого значения при помощи аргумента %

182
Перестали рассылаются пуши через ApnsPHP, кодировка

Перестали рассылаются пуши через ApnsPHP, кодировка

Всем привет, столкнулся с проблемой при отправке пушей средствами ApnsPHP

166
Сортировка массива по родителю

Сортировка массива по родителю

Нужно отсортировать массив, чтобы дочерние элементы шли сразу после родителяСложность в том, что в корне не обязательно будут стоять элементы...

214