Showing posts with label non-printable. Show all posts
Showing posts with label non-printable. Show all posts

Friday, April 18, 2008

Stripping Non-Printable Characters

Here's a quick way to strip non-printable characters in PHP. This is pretty handy for cleaning data before putting it in a DB.

$val = preg_replace('/[^\r\n\t\x20-\x7E\xA0-\xFF]/', ' ', $val)


Being a Perl compatible regexp, you can use it in your language of choice so long as it supports PCRE.

UPDATE:

Steve Laniel reminded me that you can use a Posix regexp to do roughly the same thing:

$val = preg_replace( '/[^[:print:]]/', '', $val )

This is a lot simpler for most cases; although, the patterns are slightly different (Posix is \x20-\x7E).