Bold Random sentence in between two H2s with PHP

Viewed 30

I have a WordPress blog and I would like to bold a random sentence between H2s,

Ideally, the PHP would dynamically take a random sentence (maybe using regex to find anything between a . or ?) and add a <b> </b> at the beginning and end of that sentence. Can this be achieved with PHP?

Below is an example of what I'd like to achieve, but of course, done dynamically using PHP, so I don't have to manually go into all my posts and. bold these sentences.

<h2>What is coffee?</h2>
<p><b>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua</b>. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat .</p>
<h2>How is coffee made?</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. <b>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</b> Excepteur laborum.</p>
<h2>Where can you buy coffee?</h2>
<p><b>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</b>  Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

If the "ransom" part of this question is too difficult to achieve, it would be find to bold only the 1st second or 3rd sentences between H2s.

1 Answers

I think you will have to use Regex and PHP preg_match (for example) to find sentences of a text. Then, you can choose one of the sentences given by your Regex (randomly or the first and third) and add <b> tags at the beginning and at the end for the chosen sentence.

I think this could help you.

Related