Can someone please help me add some code and help me complete this task? I need help choosing the first sentence in each set of five sentences. Then place the first sentence of each group into a paragraph. Finally display them together.
I've done most of the work on this complex task: opening the file using PHP, storing the contents in an array, splitting the array into sentences, and grouping the sentences into groups of five sentences each.
code show as below
//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)) );