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

Some array elements missing from newly chunked, different string length element array

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

到目前为止,这是我的代码:

$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!

如有任何帮助,我们将不胜感激。

如果您尝试将 $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

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

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

该模式匹配三十组 "words",然后用 \K 忘记它们,然后使用下一个 space 作为定界字符。轻松搞定。

代码:(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!',
)