preg_replace一个带有h5标签的特定字符串

I want to do the following using preg_replace:

$text = "
Lorem ipsum dolor sit amet
***consectetuer adipiscing elit
Aenean commodo ligula eget dolor
Aenean massa
"

$regex = "#('\***')(.*?)
#";
$text = preg_replace($regex1,"<h5>$2</h5>", $text);

And than the output:

Lorem ipsum dolor sit amet
<h5>consectetuer adipiscing elit</h5>
Aenean commodo ligula eget dolor
Aenean massa

So replace the three * with an opening H5 and than the first line break after the three * replace with the closing H5.

I tried many regex patterns, but with no success. As I'm not familiar with this I hope someone can help me?

Your regex was on the right track.

  • The single quotes are uneeded:

    $regex = "#('\***')(.*?)
    #";
                ↑    ↑
    
  • And each literal * needs escaping:

    $regex = "#(\*\*\*)(.*?)
    #";
    

I would also match the right in the second capture group, or use \R for any linebreak. Or perhaps even the #m flag and just the line end $ anchor.