0

I am having following tags, I want to replace class name of p tag which follows h2 tag.

    <h2>H2 Tag</h2>
    
    <p class="cn">But you forget</p>

I have tried the following

/* </h2>(.*)<p class="cn"> */

preg_match('/<\/h2>(.*)<p class="cn">/m', $input_line, $output_array);

It's not matching new line

phpnerd
  • 850
  • 1
  • 10
  • 25
  • 2
    Replace `/m` (that makes no sense in the pattern) with `/s` to make `.` match line break chars (that it does not match by default). – Wiktor Stribiżew Jul 29 '21 at 07:31
  • 2
    The regular expressions are a powerful tool but not powerful enough to parse HTML. – axiac Jul 29 '21 at 07:31
  • 2
    Use a proper parser. On php backend, [PHP Simple HTML DOM Parser](https://simplehtmldom.sourceforge.io/) does it pretty nicely. On frontend, [JS does it perfectly](https://developer.mozilla.org/docs/Web/API/Document/querySelector) – Cid Jul 29 '21 at 07:36

0 Answers0