There is a function that breaks a string into parts of a given length:
function split($str, $len = 5) { $arr = []; $length = mb_strlen($str, 'UTF-8'); for ($i = 0; $i < $length; $i += $len) { $arr[] = mb_substr($str, $i, $len, 'UTF-8'); } return $arr; } The output is an array consisting of strings up to 5 characters long.
It is necessary to modify the function so that one of the transfer conditions is a newline character / n. Ie, the transfer is made within 5 characters, but if a transfer sign is encountered, we do a break in this place. This is necessary so that the transfer does not tear the words apart.
Those, if the source string has the form,
$str = '12345/n9101111/n2'; the output should be:
1=>12345 (5) 2=>678/n (4) 3=>91011 (5) 4=>11/n (3) 5=>2 (1) Specification: For example, there is a string 150 characters long, it needs to be divided into blocks of a maximum length of 50 characters, there are hyphenations on a new line in the line. The task is to split a line into such a number of blocks with a maximum length of 50 characters in order to “break off” words as little as possible by breaking the blocks at the newlines, even if they are shorter than 50 characters. But if there are no line breaks, then break on the last space. Well, or if there are no spaces, then break on the 50th character. The screenshot here illustrates the i.stack.imgur.com/EqfmR.jpg problem