Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

list of all PHP preg_replace characters to escape

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!

like image 822
user2336671 Avatar asked Jan 27 '26 15:01

user2336671


1 Answers

You can use preg_quote():

$keywords = '$40 for a g3/400';
$keywords = preg_quote($keywords, '/');
print $keywords; 
// \$40 for a g3\/400
like image 50
treideca44 Avatar answered Jan 30 '26 03:01

treideca44



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!