-5

Hello I'm trying to parse string chars to prevent sql injection and other hacks. I don't want to use mysql_real_escape_string or other filters. I want to just use a regex and have characters A-Z 0-9 !@#$-_

I mean I could use a regex such as:

$newStr = preg_replace('/[^a-z0-9]/i', '_', $str);

but I just want to be safe and I'm not very good at regex. Thanks again guys, you really are awesome.

user1594121
  • 107
  • 1
  • 8

1 Answers1

0

This:

$newStr = preg_replace('/[^a-z0-9]/i', '_', $str);

should be:

$newStr = preg_replace('/[^a-zA-Z0-9!@#$-]/', '_', $str);

The code below should strip out:

'"/\;?"

<?php
        $newStr = preg_replace('/[^a-zA-Z0-9!@#$-]/', '_', "test\'\"\/\\\;\?");
        echo $newStr;
?>

Which produces:

test__________%
Paul Calabro
  • 1,748
  • 1
  • 16
  • 33