The middle of the HTML string viewed as text, after/before nearest sentence.
Example:
$str = 'First sentence. Second sentence! <a title="Hello. World">
Third sentence. </a> Fourth sentence. Fifth sentence?';
$middle = strlen($str) / 2;
what I did is to append a keyword to all stop characters like ., ! or ?, but only to the ones outside HTML tags:
$str = 'First sentence. Second sentence! <a title="Hello. World">
Third sentence. </a> Fourth sentence. Fifth sentence?';
$str = preg_replace_callback("/(\.|\?|\!)(?!([^<]+)?>)/i", function($matches){
return $matches[1].'<!-- stop -->';
}, $str);
This turns $str into:
First sentence.<!-- stop --> Second sentence!<!-- stop --> <a title="Hello.
World"> Third sentence.<!-- stop --> </a> Fourth sentence.<!-- stop --> Fifth
sentence?<!-- stop -->
Now, how can I find the <!-- stop --> sub-string that’s nearest to the $middle position, and insert my text before it?
Is there a way I can find the position of $matches[1] inside my preg_replace callback relative to the whole string? That way I could just compare it with $middle
how is this?