简体   繁体   中英

Regex with preg_replace in PHP

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
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM