I'm trying to insert an HTML tag after the first sentence in each paragraph of a given document.
The code I came up with ( I am not a programmer) is working.
The $insert_pos
is the position where a tag was last inserted. It is needed because there is more than one paragraph in most of the documents.
Now I need to also check for "?" (and possibly "!").
$insert_pos = strpos($content, ".", $insert_pos) + 1;
$content= substr_replace( $content, "</tag>", $insert_pos,0 );
Some context:
Per CMS, a paragraph is generated with </br><br />
. So a document will have this format:
Lorem ipsum dolor sit amet, consetetur sadipscing elitr sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. </br><br />Lorem ipsum. Lorem ipsum dolor sit amet, consetetur sadipscing elitr sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. </br><br />voluptua.
I need each sentence in between <br />
and .
OR !
OR ?
to become the text inside of a <h3>
tag. So in the format of <h3>Lorem ipsum.</h3>
To apply the replacement to each new paragraph (start of the content or sentence after two break tags, match those occurrences, then use \K
to "restart the match". Then match zero or more characters that are not in the punctuation list then a sentence-ending punctuation. $0
is the matched substring which is used in the replacement string so that no content is actually lost.
Code: (Demo)
$content = "What in the world? I don't know.<br><br>This is paragraph number 2! What a fascinating read.<br><br>No matter how many paragraphs, look for one of the three sentence ending punctuations after a fully empty line. Good stuff!";
$content = preg_replace('~(?:^|<br><br>)\K[^.?!]*[.?!]~', '<h3>$0</h3>', $content);
// ^^^^^^^^-- </br><br /> to be more specific
echo $content;
Output:
<h3>What in the world?</h3> I don't know.<br><br><h3>This is paragraph number 2!</h3> What a fascinating read.<br><br><h3>No matter how many paragraphs, look for one of the three sentence ending punctuations after a fully empty line.</h3> Good stuff!