-1

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Yerain
  • 1
  • What have you tried so far? – user3783243 Sep 22 '22 at 11:56
  • Hi, and thank you for the reply! :) I have been exploring PHP Rejex, but that's about it. – Yerain Sep 22 '22 at 12:02
  • **Regex + HTML === failure**. Don't do it. Even if you get it to work for a few examples of HTML, it will be easily broken by other valid HTML. You should parse the HTML with a DOM parser. Read this for some examples. https://stackoverflow.com/questions/18349130/how-to-parse-html-in-php – O. Jones Sep 22 '22 at 12:04
  • What is the expected behavior if a sentence of `An X. laevis is a frog.` is encountered? – user3783243 Sep 22 '22 at 12:14
  • @user3783243 Great question. I guess id have to add a stipulation. So, like, from

    to the first period or question mark is a sentence, and from the first to the second period is also a sentence. BUT it can only bold ONE of them.

    – Yerain Sep 22 '22 at 12:57
  • You should parse the document in that case then run a regex based on the content of the `p` nodes. In the above example I'd expect `An X. laevis is a frog.` is that correct? (Also `strong` should be used for bolding, not `b`.) – user3783243 Sep 22 '22 at 13:12
  • Thank you for the help. May I have a quick example to get the ball rolling? I can do the research and work myself; I 'djust like to get a little idea of what you mean in code form. – Yerain Sep 22 '22 at 13:18
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 22 '22 at 19:25

1 Answers1

0

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.

Bastien
  • 118
  • 13