We can remove non printable characters in string by using preg_replace function in php, It's quite simple. Just follow the example,
$string = preg_replace('/[x00-x08x0Bx0Cx0E-x1Fx80-x9F]/u', '', $string);

Sometimes you may need to strip out new lines,tab spaces too. If your requirement is that then below example will help you.
$string = preg_replace('/[x00-x1Fx80-x9F]/u', '', $string);

x in the above examples denotes hexadecimals charaters. So we are saying,  remove hexadecimals x00-x1F and so on..

Refer this website for more info on hexadecimal and ascii characters http://www.asciitable.com


Comments (1)
  1. Image
    PHP Course - Reply

    August 12, 2020

    Grate post. Thank You for sharing useful information it is very beneficial to us.

Leave a Comment

loader Posting your comment...