How do we move the line on the gap?



  • We need to move the lines (write in the mass) along the gap in the given size of the line length.

    Example:

    $text = 'Возвращает TRUE в случае успешного завершения или FALSE в случае возникновения ошибки. Если вызвана статически, возвращает объект класса DOMDocument или FALSE в случае возникновения ошибки.';
    class MyClass {
    
    public $output = array();
    
    public function text($text, $string_length){
         print_r($output);
    }
    

    }
    $obj = new MyClass();
    $obj->text($text, 20);

    shall be obtained:

    Array ( [0] => Возвращает TRUE в [1] => случае успешного завершения [2] => или FALSE в случае возникновения [3] => ошибки. Если вызвана статически, [4] => возвращает объект класса DOMDocument [5] => или FALSE в случае возникновения )

    I've decided this way:

    class MyClass {

    public $output = array();
    
    public function text($text, $string_length){
    
        $i = 0;
        foreach(explode(' ', $text) as $val){
            if(!empty($this->output[$i])){
                if(iconv_strlen($this->output[$i], 'utf-8') < $string_length){
                    $this->output[$i] .= $val.'  ';
                } else {
                    ++$i;
                    if(isset($this->output[$i])){$this->output[$i] .= $val.'  ';}
                }
            } else {
                $this->output[$i] = $val.' ';
            }
        }
        print_r($this->output);
    }
    

    }

    $obj = new MyClass();
    $obj->text($text, 20);

    But the result is not true:

    Array ( [0] => Возвращает TRUE в [1] => успешного завершения [2] => FALSE в случае возникновения [3] => Если вызвана статически, [4] => объект класса DOMDocument [5] => FALSE в случае возникновения )

    Lost starting array [1]
    I mean, it should be: [0] => Возвращает TRUE в [1] => СЛУЧАЕ успешногоAnd the word SERVICES lost and placed in place completion

    I don't want my algorithm to be the best, but I've already tried a few different ways, and this one's closest to the truth.



  • Bringing the author ' s ideas to a logical conclusion.

    1. Option No. 1.
      Converting the line into the CP1251 single-bite code.
      Correct challenge to wordwrap in CP1251.
      Conversion to the original code.
      Packing up a ready line in the area.

      class MyClass {
          public $output = array();    
      
      public function text($text, $string_length){
          $code_original = mb_detect_encoding($text);
          $text_cp1251 = mb_convert_encoding($text, "CP1251");
          $wrap_cp1251 = wordwrap($text_cp1251, 20,"<br>");
          $wrap_original = mb_convert_encoding($wrap_cp1251, $code_original, "CP1251");
          $this->output = explode("<br>", $wrap_original);
          print("<pre>"); print_r($this->output); print("</pre>");
      }
      

      }

      $text = 'Возвращает TRUE в случае успешного завершения или FALSE в случае возникновения ошибки. Если вызвана статически, возвращает объект класса DOMDocument или FALSE в случае возникновения ошибки.';
      $obj = new MyClass();
      $obj->text($text, 20);

    2. Option No. 2 - Reassignment of the programme from the text of the question.
      The sum of the signs in the row is crumbled, and the next line is changed when the "example" with the new word gives an excessive length of the line.

      class MyClass {

      public $output = array();    
      
      public function text($text, $string_length){
      
          foreach(explode(' ', $text) as $val){
              $len = iconv_strlen($val, 'utf-8')+1;
              if(empty($sum_prev)){
                  $sum_prev = $len;
                  $out_prev = $val;
                  continue; 
              }
              if(($sum_prev += $len) < $string_length+2){
                  $out_prev .= (' '.$val);
              } else { 
                  array_push($this->output, $out_prev);
                  $sum_prev = $len;
                  $out_prev = $val;
              }
          }
          array_push($this->output, $out_prev);
          print("<pre>"); print_r($this->output); print("</pre>");
      }
      

      }

      $text = 'Возвращает TRUE в случае успешного завершения или FALSE в случае возникновения ошибки. Если вызвана статически, возвращает объект класса DOMDocument или FALSE в случае возникновения ошибки.';
      $obj = new MyClass();
      $obj->text($text, 20);

    The results are completely matched:

    Array
    (sighs)
    [0] = sector Getting TRUE back in.
    [1] = scope of success
    [2] = state completion or FALSE
    [3] = limit in case
    [4] = source
    [5] = fundamental error. If summoned
    [6] = characteristic,
    _
    [8] = Class DOMDocument
    [9] = constituent or FALSE in the case of
    [10] = source
    [11] = fundamental errors.
    )




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2