在PHP中使用preg_replace的正则表达式

It's a long time ago that I haven't played with PHP and regex and I'd like to find a regex that would do the following work.

My string contains :

<pre code="...">some piece of code</pre> other non code content <pre code="...">some piece of code</pre> other non code content...

The goal is to replace all the <pre>code</pre> by&

code
...`

Where "code" inside the <pre>&</pre> should also be escaped with htmlspecialchars...

I've already tried a few regex but didn't succeed.

Any idea?

Thanks

Generally, using RegEx to parse HTML is a bad idea. There are plenty of simple scenarios, where RegEx is enough to solve a particular problem, and that is great.

I would argue that in your case using RegEx is a bad idea, it will not cover all cases and it is likely insecure. You are possibly trying to prevent XSS vulnerabilities, and RegEx based solutions are always error-prone.

But for completeness sake:

preg_replace_callback(
    '/(<\\s*pre(?:\\s[^>]+)?>)(.*?)(<\\/\s*pre\s*>)/',
    function ($match) {
        return $match[1].htmlspecialchars($match[2]).$match[3];
    },
    $html
);