Stewart Robinson Stewart Robinson - 1 month ago 11
PHP Question

PHP: How to remove all non printable characters in a string?

I imagine I need to remove chars 0-31 and 127,

Is there a function or piece of code to do this efficiently.

Answer

Something like this should do it

$string = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $string);

It matches anything in range 0-31, 128-255 and removes it.