检查字符串和htmlentities预先的一切

Rob*_*ert 2 php regex preg-replace

我正在尝试编写评论表单.

我正在考虑使用strstr来查找是否<pre>存在以及是否确实使用htmlentities将其中的所有内容都转换为html.

但我遇到的问题是实际上在转换前的所有内容.

我的代码变为现实,但如何替换.

我正在看preg_replace但我的正则表达式真的很差:)

例:

$string = 'This is a form with <pre>Everything in this needs to be htmlentities() </pre> and everything outside needs to be normal.';
Run Code Online (Sandbox Code Playgroud)

bon*_*dle 5

如果我的问题是正确的,那么这应该有效:

$string_new = preg_replace_callback(
    '#<pre>([\\s\\S]+?)</pre>#',
    create_function(
        '$input',
        'return "<pre>".htmlentities($input[1])."</pre>";'
    ),
    $string
);
Run Code Online (Sandbox Code Playgroud)