I am trying to only certain characters to be saved to the database from input of a textarea. The input is going to a variable called $vBody. What I have below works:

$vBodyCleaned = reg_replace('[^A-Za-z0-9~!@#$%&*()-_=+\'|:;",./? ]','',$vBody);

except for the fact that the client wants to preserve the line feeds and carriage returns. The above code strips them out. I have searched for an answer here and elsewhere. The problem is that I'm looking at it from the "allow" side rather than the "disallow" side. How do I add support for line feeds and carriage returns in my code?

Thanks!

I am trying to only certain characters to be saved to the database from input of a textarea. The input is going to a variable called $vBody. What I have below works:

$vBodyCleaned = reg_replace('[^A-Za-z0-9~!@#$%&*()-_=+\'|:;",./? ]','',$vBody);

except for the fact that the client wants to preserve the line feeds and carriage returns. The above code strips them out. I have searched for an answer here and elsewhere. The problem is that I'm looking at it from the "allow" side rather than the "disallow" side. How do I add support for line feeds and carriage returns in my code?

Thanks!

Is reg_replace() a user defined function? or is it ereg_replace?

I'd used preg_replace instead. Its faster.

I don't understand your regex since it looks like a you're doing exclusion and not inclusion to me. Anyway, if you do inclusion, then just include the line break: \n in the regex. If exclusion, then don't include it.

\s is the control character for whitespace (tabs, spaces, and newlines) so just add that to your regex which is also not working as you think it should given that you aren't escaping regex control characters. It should be something along the lines of

$something = preg_replace('/[^\w~!@#\$%&\*\(\)\-=\+'\|:;",\./\?\s]/','',$something_else);
This article has been dead for over six months. Start a new discussion instead.