作者:mobiledu2502911457 | 来源:互联网 | 2023-09-13 14:58
我正在尝试将字符串数组转换为新的字符串数组,并通过相应地附加同级项来更改每个元素的字数。但是我遇到的问题是先前数组的某些部分未按要求进行转换。
到目前为止,这是我的代码:
$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."
";
$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,and taunted.
His eyes were filled with sincerity as he looked at Yang Shiyue and said,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,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!
任何帮助将不胜感激。
如果您尝试将java -Xmx1g -jar ./h2o.jar
的元素重新组织成不同的单词长度,则最简单的解决方案是创建一个包含所有单词的数组(通过将现有字符串重新组合为一个,然后再次拆分),然后使用set it properly for/from R将其拆分为$text_array
个单词组。例如:
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_chunk
,
正则表达式为您提供了一种非常简洁的技巧。
此一线将在每第30个字(非空白子字符串)之后在空白字符上拆分连接的字符串。
该模式匹配三十个“单词”的集合,然后用\K
忘记它们,然后使用下一个空格作为定界字符。简单。
代码:(Demo)
var_export(preg_split('~\S+(?: \S+){29}\K ~',$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!',)