I have been scratching my head on this but cannot work out a solution.
Let's say you have a text of 5000 characters, I would like to split it into blocks of less than 500 characters, but, without breaking a single sentence. eg: if a paragraph is let's say 550 words and the last sentence stops at 550 characters but start at 450 characters, I would like to save this particular block to a maximum of 450 characters(this way no sentences are broken).
Any idea how to achieve this please?
My goal is to save each block into an array so I can work on them separately.
I was thinking about using preg_split, sum the outputs, and if the sum is above 500 characters, remove the last sum. But.....I find it difficult to separate the sentences without mistakes.
Any idea what preg_split rules I should use to make sure that every single sentences are well separated?
I tried to use this tool but cannot get it to give me the right output: https://www.phpliveregex.com/#tab-preg-split
Thanks
First of all: Thank you for the nice question!
The solution is not really stable and you have to adjust in the future. But it will shows you the possible way to archive this.
Split your text into the individual sentences and save each sentence as an element in an array. This way you can determine the length of the sentences when iterating the array. As long as the sentence and the previous sentence are smaller than the maximum block length, put the string into a temporary variable. As soon as the length of the text of the temporary variable + the length of the current record are greater than the maximum block length, the record is stored in a new array as a block.
Output