There is a string of n characters. For example, the line: "I am a noob in php" it is necessary to divide it into characters and write them in a separate cell. and set the number of columns. For example, 4. And there should be as many lines as needed in order to fit all the text so that it visually looks like this: enter image description here

if there are 11 or more columns, then everything will be in one row.

it is clear to the characters how to break, but how to indicate how much I need to make columns in this array I just do not understand. Poke your nose at least or point at a clever idea.

UPD: if you fill in with your hands, it will come out like this, but how to do it, what would be the most lined up for a given number?

<?php echo "<pre>"; $text = 'очень большой текст'; $text = preg_split('//u',$text,-1,PREG_SPLIT_NO_EMPTY); $key = "привет"; $count = mb_strlen($key); echo "Должно быть: ".$count. ' столбцов.'; $a = array('о','ь','л','й','к',' '); $b = array('ч',' ','ь',' ','с',' '); $c = array('е','б','ш','т','т',' '); $d = array('н','о','о','е',' ',' '); $THN = array($a, $b, $c, $d); for($i=0; $i < count($THN); $i++) { for($q=0; $q < count($THN[$i]); $q++) { echo "|".$THN[$i][$q]; } echo "<br>"; } ?> 

UPD 2: I dig towards multidimensional arrays, but again with handles. fill in then i fill in the loop, but how to indicate that i should have exactly! 4! array, even if I obviously indicate this in the code. How exactly to create a new array in a loop? Is it indicated number 4 or 100?

 #сюда как бы нужно заполнить массив $text, но не ручками $myArray = array( array('о','ь','л','й','к',' '), array('ч',' ','ь',' ','с',' '), array('е','б','ш','т','т',' '), array('н','о','о','е',' ',' ') ); for ($i = 0; $i < 4; $i++){ for ($j=0; $j <6; $j++){ echo ' | '.$myArray[$i][$j]; } echo '<br />'; } 

Thank you in advance!

  • $ columns = 4; Does not help? - Vladimir Martyanov
  • @ Vladimir Martiyanov didn’t quite understand what you mean - sbaikov
  • Recently there was such a question. - Visman
  • @Visman can link? - sbaikov

1 answer 1

 function make_letters_field( $string, $columns = 1 ){ $char_array = str_split( $string ); $row_count = ceil( count($char_array) / $columns ); $last_big_column_key = $columns + count($char_array) - $row_count * $columns; $additional_items = 0; $field = array(); for($i = 0; $i < count($char_array); $i++){ if( floor($i / $row_count) >= $last_big_column_key ){ $additional_items = floor( ($i + $additional_items) / $row_count ) - $last_big_column_key + 1; } $i_key = ($i + $additional_items) % $row_count; if( $additional_items ){ $i_key--; } $j_key = floor( ($i + $additional_items) / $row_count ); $field[$i_key][$j_key] = $char_array[$i]; } // заполняем пустые поля for($i = 0; $i < $row_count; $i++){ for($j = 0; $j < $columns; $j++){ if( ! isset($field[$i][$j]) ){ $field[$i][$j] = ''; } } } return $field; } $example_string = "Some text for example"; // строка для примера $val = make_letters_field($example_string, 4); // вызов функции. количество колонок - 4 // вывод массива с результатом for($i = 0; $i < count($val); $i++){ for($j = 0; $j < count($val[$i]); $j++) echo $val[$i][$j]; echo PHP_EOL; } 
  • $val = make_letters_field($example_string, 10); when specifying 10 columns, your code displayed 7 in 3 lines. - Ordman
  • Yes you are right. I did not consider the situation when there could be a lot of empty cells in the last line. In general, corrected the message. Try;) - Sergey Khomenko
  • @SergeyKhomenko why the encoding flies, if you specify Russian characters? - sbaikov
  • @sbaikov in the second line replace $char_array = str_split( $string ); on $char_array = preg_split('//u', $string, null, PREG_SPLIT_NO_EMPTY); to support multi-byte encodings (str_split splits a string by byte, not by character) - Sergey Khomenko
  • @SergeyKhomenko. If you specify the text "very large text" 6 columns and 4 lines, then the first column counts everything normally, and the second one already goes by 3 characters, and the bottom clogs with problems - sbaikov