In php I have the following reqular expression:
$regexp = "/^([-a-z0-9.,!@'?_-\s])+$/i";
Im trying to validate my websites contact form (specifically the message field) to ensure no nasty code has been entered. The problem I am having is that certain normal punctuation and characters I need to allow, but I'm worried they could be used to insert malicious code.
For any character not obeying the expression above, I would like to replace it to make it safe. Two questions:
- How do I do the replacement?
- What should I replace the character with? For example I am not allowing parenthesis
( )
. would it be best practice to replace like this"(" ")"
or maybe\( \)
?
EDIT
The data will be sent to an email address and saved to a database