代码之家  ›  专栏  ›  技术社区  ›  Madushan Perera

新分块的不同字符串长度的元素数组中缺少一些数组元素

  •  -2
  • Madushan Perera  · 技术社区  · 6 年前

    我正在尝试将字符串数组转换为新的字符串数组,通过相应地追加同级项来更改每个元素的字数。但我遇到的问题是前一个数组的某些部分没有按要求进行转换。

    这是我目前的代码:

    $text_array = ['He needs to cultivate in order', 
    'to be at the fourth level of the', 
    'Martial Body Stage. Does he have inner energy?"', 
    'Everyone jeered, laughed, and taunted.', 
    'Qin Yun turned deaf ear to their taunts.',  
    'His eyes were filled with sincerity as he',  
    'looked at Yang Shiyue and said, "Teacher,', 
    'I only formed my elemental energy this morning.', 
    'I still not familiar with the control of', 
    'my elemental energy and inner energy."',  
    'After the empress heard the jeers from the',  
    'crowd, she let out a sigh of relief and',  
    'sneered, "This is only a little bit of',  
    'inner Qi that you forced out.', 
    'You have not yet stepped',  
    'into the fourth level',  
    'of the Martial Body realm and have no',  
    'chance of breaking through. embarrass yourself!'];
    
            $last_converted_index = 0;
            $new_string_array = [];
            $single_valid_length_string = '';
            foreach (array_slice($text_array, $last_converted_index) as $item) {
    
                if (str_word_count($single_valid_length_string . $item) < 30) {
    
                    $single_valid_length_string .= $item . ' ';
                    $last_converted_index++;
    
                } else {
                    $new_string_array[] = $single_valid_length_string."<br><br>";
                    $single_valid_length_string = '';
                }
    
            }
    
            echo implode($new_string_array);
    

    我现在得到的结果是:

    He needs to cultivate in order to be at the fourth level of the Martial Body Stage. Does he have inner energy?" Everyone jeered, laughed, and taunted.
    
    His eyes were filled with sincerity as he looked at Yang Shiyue and said, "Teacher, I only formed my elemental energy this morning.
    
    my elemental energy and inner energy." After the empress heard the jeers from the crowd, she let out a sigh of relief and 
    
    

    我的预期结果是:

    He needs to cultivate in order to be at the fourth level of the Martial Body Stage. Does he have inner energy?" Everyone jeered, laughed, and taunted.
    
    His eyes were filled with sincerity as he looked at Yang Shiyue and said, "Teacher, I only formed my elemental energy this morning.
    
    my elemental energy and inner energy." After the empress heard the jeers from the crowd, she let out a sigh of relief and 
    
    sneered, "This is only a little bit of inner Qi that you forced out.You have not yet stepped into the fourth level
    
    of the Martial Body realm and have no chance of breaking through. embarrass yourself!
    
    

    任何帮助都将不胜感激。

    2 回复  |  直到 6 年前
        1
  •  1
  •   Nick SamSmith1986    6 年前

    如果你想重新组织 $text_array 要有一个不同的字长,最简单的解决方案是创建所有单词的数组(通过将现有字符串重新组合成一个,然后再拆分)然后使用。 array_chunk 把它分成几组 n 话。例如:

    function change_words_length($text, $numwords) {
        $words = explode(' ', implode(' ', $text));
        $output = array();
        foreach (array_chunk($words, $numwords) as $array) {
            $output[] = implode(' ', $array);
        }
        return $output;
    }
    
    print_r(change_words_length($text_array, 10));
    print_r(change_words_length($text_array, 30));
    

    输出:

    Array
    (
        [0] => He needs to cultivate in order to be at the
        [1] => fourth level of the Martial Body Stage. Does he have
        [2] => inner energy?" Everyone jeered, laughed, and taunted. Qin Yun turned
        [3] => deaf ear to their taunts. His eyes were filled with
        [4] => sincerity as he looked at Yang Shiyue and said, "Teacher,
        [5] => I only formed my elemental energy this morning. I still
        [6] => not familiar with the control of my elemental energy and
        [7] => inner energy." After the empress heard the jeers from the
        [8] => crowd, she let out a sigh of relief and sneered,
        [9] => "This is only a little bit of inner Qi that
        [10] => you forced out. You have not yet stepped into the
        [11] => fourth level of the Martial Body realm and have no
        [12] => chance of breaking through. embarrass yourself!
    )
    Array
    (
        [0] => He needs to cultivate in order to be at the fourth level of the Martial Body Stage. Does he have inner energy?" Everyone jeered, laughed, and taunted. Qin Yun turned
        [1] => deaf ear to their taunts. His eyes were filled with sincerity as he looked at Yang Shiyue and said, "Teacher, I only formed my elemental energy this morning. I still
        [2] => not familiar with the control of my elemental energy and inner energy." After the empress heard the jeers from the crowd, she let out a sigh of relief and sneered,
        [3] => "This is only a little bit of inner Qi that you forced out. You have not yet stepped into the fourth level of the Martial Body realm and have no
        [4] => chance of breaking through. embarrass yourself!
    )
    

    Demo on 3v4l.org

        2
  •  0
  •   mickmackusa Tom Green    6 年前

    正则表达式提供了一种非常简洁的技术。

    这一行将在每30个单词(非空白子字符串)后拆分空白字符上的连接字符串。

    这个模式匹配30个“单词”,然后用 \K ,然后使用下一个空格作为定界字符。做起来容易。

    代码:( Demo )

    var_export(preg_split('~\S+(?: \S+){29}\K ~', implode(' ', $text_array)));
    

    输出:

    array (
      0 => 'He needs to cultivate in order to be at the fourth level of the Martial Body Stage. Does he have inner energy?" Everyone jeered, laughed, and taunted. Qin Yun turned',
      1 => 'deaf ear to their taunts. His eyes were filled with sincerity as he looked at Yang Shiyue and said, "Teacher, I only formed my elemental energy this morning. I still',
      2 => 'not familiar with the control of my elemental energy and inner energy." After the empress heard the jeers from the crowd, she let out a sigh of relief and sneered,',
      3 => '"This is only a little bit of inner Qi that you forced out. You have not yet stepped into the fourth level of the Martial Body realm and have no',
      4 => 'chance of breaking through. embarrass yourself!',
    )
    
    推荐文章