Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx Whitelist Problem

I'm a little confused. I am running all my inputs through a basic sanitize function I write to only allow certain characters, but characters such as [] are still being allowed.

function sanitize($input) {
$pattern = "/[^a-zA-z0-9_-]/";
$filtered = preg_replace($pattern, "", $input);
return $filtered;}

Any idea why it's doing that?

like image 772
Jamie Redmond Avatar asked Dec 12 '25 04:12

Jamie Redmond


1 Answers

You have a typo in your pattern string that's causing the problem

/[^a-zA-z0-9_-]
You want A-Z instead.

btw: you might be interested in the character class [:alnum:] and/or the PCRE_CASELESS modifier

like image 131
VolkerK Avatar answered Dec 14 '25 18:12

VolkerK



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!