list of all PHP preg_replace characters to escape

2019-05-26 02:50发布

Where can find a list of all characters that must be escaped when using preg_replace. I listed what I think are three of them in the array $ESCAPE_CHARS. What other ones am I missing.

I need this because I am going to be doing a preg replace on a form submission.

So ie.

$ESCAPE_CHARS = array("#", "^", "[");

    foreach ($ESCAPE_CHARS as $char) {
    $_POST{"string"} = str_replace("$char", "\\$char", $_POST{"string"});
    }
    $string = $_POST{"string"};

$test = "string of text";

$test = preg_replace("$string", "<b>$string</b>", $test);

Thanks!

1条回答
女痞
2楼-- · 2019-05-26 03:22

You can use preg_quote():

$keywords = '$40 for a g3/400';
$keywords = preg_quote($keywords, '/');
print $keywords; 
// \$40 for a g3\/400
查看更多
登录 后发表回答