You can use PHP's trim function to remove non-printable characters:
$string = trim($string, "\x7f..\xff\x0..\x1f");