有人可以幫我加入一些程式碼並幫我完成這個(gè)任務(wù)嗎?我需要幫助來選擇每組五個(gè)句子中的第一句。然後將每組的第一句放入一個(gè)段落。最後將它們一起顯示出來。
我已經(jīng)完成了這個(gè)複雜任務(wù)的大部分工作:使用PHP開啟文件,將內(nèi)容儲(chǔ)存在陣列中,將陣列分割成句子,並將句子分組為每組五個(gè)句子。
程式碼如下
//Reading file contents $text = file_get_contents( 'majmunskikompjuter.txt' ); echo $text; //Divide the text into sentences $result = preg_split('/(?<=[.?!;:])s+/', $text, -1, PREG_SPLIT_NO_EMPTY); print_r($result); //Divide the text into equal parts containing 5 sentences each $input_array = $result; print_r(array_chunk($input_array, 5, true)); //Select first sentence from each part of the divided text and display together ???? Need help
例如,您可以在 array_chunk 中省略最后一個(gè)參數(shù)以不保留鍵,并使用 array_map 返回從 array_chunk 返回的數(shù)組的第一個(gè)條目。
然后使用 implode 和一個(gè)空格。
以下使用箭頭函數(shù)的示例:
$result = preg_split('/(?<=[.?!;:])\s+/', $text, -1, PREG_SPLIT_NO_EMPTY); $result = implode(" ", array_map( fn($arr) => $arr[0], array_chunk($result, 5)) );