Problem with images URL after filter applying

<?php
$string = 'Sentence 1. Sentence 2? Sentence 3! Sentence 4... Sentence 5… Sentence 6! Sentence 7. Sentence 8. Sentence 9… Sentence 10... Sentence 11. ';

$sentences_per_paragraph = 3; // settings

$pattern = '~(?<=[.?!…])\s+~'; // some punctuation and trailing space(s)

$sentences_array = preg_split($pattern, $string, -1, PREG_SPLIT_NO_EMPTY); // get sentences into array

$sentences_count = count($sentences_array); // count sentences

$output=""; // new content init

// see PHP modulus
for($i = 0; $i < $sentences_count; $i++) {
    if($i%$sentences_per_paragraph == 0 && $i == 0) { //divisible by settings and is first
        $output .= "<p>" . $sentences_array[$i] . ' '; // add paragraph and the first sentence
    } elseif($i%$sentences_per_paragraph == 0 && $i > 0) { //divisible by settings and not first
        $output .= "</p><p>" . $sentences_array[$i] . ' '; // close and open paragraph, add the first sentence
    } else {
        $output .= $sentences_array[$i] . ' '; // concatenate other sentences
    }
}

$output .= "</p>"; // close the last paragraph

echo $output;

Note: It’s a very rough code that does not check the deeper problems.

For more info: