我正在尝试将字符串数组转换为新的字符串数组,并通过相应地附加同级项来更改每个元素的字数。但是我遇到的问题是先前数组的某些部分未按要求进行转换。
到目前为止,这是我的代码:
$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!
)