Hello. It is necessary, using PHP 5.6, to break the text, the length of which is more than 3000 characters into pieces no more than 500 characters. But it is necessary to take into account the proposals. Text may contain tags. For example, we have the text, it has 1398 characters:

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.

It is necessary to get an array at the output:

 $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." ]; 

Tell me how to do it best.

  • Well, look for the text of the substring point and space . Yes, remember the position. If the next one is more than 500 - drop everything from the beginning to the previous position in the file, cut off the skipped, and continue until the text ends. Do not forget to provide an option when one sentence is more than 500 characters. And think about what to do with all there exclamation and interrogative. - Akina

1 answer 1

Make your own beauty ...

 $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);