2

I am in the middle of trying to code a comment form.

I was thinking of using strstr to find if <pre> exists and if it does turn everything within it into html safe using htmlentities.

But I am coming into a problem of actually converting everything within the pre.

My code becomes true but how to replace.

I was looking at preg_replace but my regex is really poor at best :)

Example:

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

1 回答 1

5

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

$string_new = preg_replace_callback(
    '#<pre>([\\s\\S]+?)</pre>#',
    create_function(
        '$input',
        'return "<pre>".htmlentities($input[1])."</pre>";'
    ),
    $string
);
于 2013-07-27T12:37:07.483 回答