php - 新分块的不同字符串长度元素数组中缺少一些数组元素
问题描述
我正在尝试将字符串数组转换为新的字符串数组,通过相应地附加同级项来更改每个元素的字数。但是我遇到的问题是先前数组的某些部分没有按要求进行转换。
到目前为止,这是我的代码:
$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!
)
推荐阅读
- google-apps-script - 通过 Google Apps 脚本创建包含图表但不包含背景数据的 PDF
- unix - 从第二列开始替换
- html - 盒子没有正确对齐
- c# - 找不到路径的控制器或未在 MVC 中实现 IController
- javascript - External Javascript not called in HTML
- cloud-foundry - 如何在cloudfoundry cf-cli中显示cpu使用情况
- wso2 - API 网关未调用全局中介扩展 - WSO2 AM 2.2.0
- css-selectors - Nightwatch 使用独特的 css 选择器查找多个元素
- python - Django&Python,抓取页面填满数据库
- android - 使用 OpenGL ES 2.0 渲染多个像素