search

Home  >  Q&A  >  body text

PHP select first sentence from each part of split text and display them together


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


P粉032977207P粉032977207554 days ago444

reply all(1)I'll reply

  • P粉132730839

    P粉1327308392023-07-18 16:23:00

    例如,您可以在 array_chunk 中省略最后一个参数以不保留键,并使用 array_map 返回从 array_chunk 返回的数组的第一个条目。

    然后使用 implode 和一个空格。

    以下使用箭头函数的示例:

    $result = preg_split('/(?<=[.?!;:])\s+/', $text, -1, PREG_SPLIT_NO_EMPTY); $result = implode(" ", array_map( fn($arr) => $arr[0],
        array_chunk($result, 5))
    );

    reply
    0
  • Cancelreply