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

    2 answers 2

     $str = "12345\n9101111\n2"; preg_match_all('~.{1,5}\n?~', $str, $m); $ret = array_map(function($s){ return rtrim($s, "\n"); }, $m[0]); var_dump($ret); 

    Returns

     array(4) { [0]=> string(5) "12345" [1]=> string(5) "91011" [2]=> string(2) "11" [3]=> string(1) "2" } 
    • Total Pusher, thank you, but a little bit wrong. Most likely, I set the task incorrectly. It is necessary to break the line so that, if possible, the words and sentences do not break, and the transfer was performed according to the maximum "\ n". Maybe the screenshot will be clearer)) [! [Screenshot] [1]] [1] [1]: i.stack.imgur.com/EqfmR.jpg Ideally, this is so that the 12th item is already in the new block ( ) - Alexey Bondar
    • Rephrase the original question. - Total Pusher
    • For example, there is a string 150 characters long, it needs to be broken into blocks with a maximum length of 50 characters, in the line there are hyphens on a new 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 problem of i.stack.imgur.com/EqfmR.jpg - Alexey Bondar
    • Please correct the original question at the top of the page. Shots also insert here. Otherwise, your question will not be interesting to anyone and will drown - Total Pusher
     function chunk_split_unicode($str, $l = 5, $e = "\r\n", $last = FALSE) { $tmp = array_chunk(preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY), $l); $res = []; $count = (count($tmp) - 1); foreach ($tmp as $k => $t) { $res[] = (($count != $k) OR ($last)) ? implode('', $t) : implode('', $t) . $e; } return $res; } function new_split($str, $len = 5) { $arr = []; $str = explode(PHP_EOL, $str); $count = (count($str) - 1); foreach ($str as $k => $value) { $arr = array_merge($arr, chunk_split_unicode($value, $len, PHP_EOL, ($count != $k) ? FALSE : TRUE)); } return $arr; } $str = '12345678 9101111 2'; var_dump(new_split($str));