How can I remove all non-printable characters in a string in PHP?

PHP

Example :
Input: str = "Geeks for Geeks" Output: Geeks for Geeks 
Note.Symbols newlines (/ n) and tabs (/ t) are not printable characters.Method 1: Using a generic regular expression:There are many regular expressions available. The best solution - remove all non-ASCII characters from the input string, which can be done with this preg_replace.Example :
// PHP program to remove all unprintable
/ / character from a string 
// A string with non-printable characters $str = "Geeks šžfor ÂGee ks" ;  
// Using the preg_replace method to remove all
// non-printable character from string $str = preg_replace ( `/ [x00-x1Fx80-xFF] /` , `` , $str );  
// Show change line echo ( $str );  
?>
Exit:
Geeks for Geeks 
Method 2: Usethe regular expression print.Another possible solution - use the regular expression print . The regular expression [: print:]means "any printable character" .Example :  

/ / PHP program to remove all non-printable
// character from string 
// A string with an unprintable character $str = " Geeks šžfor ÂGee ks " ;  
// Using the preg_replace method to remove all
// non-printable character from string $str = preg_replace ( `/ [[: ^ print:]] /` , `` , $str );  
// Show change line echo ( $str );  
?>
Exit:
Geeks for Geeks 




Get Solution for free from DataCamp guru